Getting a node in JTree
Asked Answered
O

3

9

Simple question. I have a TreePath to a node in my JTree. How can I convert this TreePath to the DefaultMutableTreeNode the TreePath points too?

Odilo answered 25/2, 2011 at 16:57 Comment(0)
C
14

You should be able to call getLastPathComponent on the TreePath and cast that for a TreeNode or DefaultMutableTreeNode and be good to go.

See: http://download.oracle.com/javase/6/docs/api/javax/swing/tree/TreePath.html#getLastPathComponent%28%29

Campagna answered 25/2, 2011 at 17:12 Comment(0)
I
8

If your treemodel consists of DefaultMutableTreeNodes you may just use node=(DefaultMutableTreeNode)path.getLastPathComponent();

Invert answered 25/2, 2011 at 17:13 Comment(0)
B
2

model is a DefaultTreeModel

private TreePath getTreePath(TreeNode node) {
    TreeNode[] nodes = model.getPathToRoot(node);
    TreePath path = new TreePath(nodes);
    return path;
}
Brier answered 20/5, 2015 at 14:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.