Getting node in JTree

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

+5
source share
3 answers

You must be able to call getLastPathComponentin TreePathand drop it for TreeNodeor DefaultMutableTreeNodeand be good to go.

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

+10
source

If your treemodel consists of DefaultMutableTreeNodes, you can simply use node=(DefaultMutableTreeNode)path.getLastPathComponent();

+7
source

- DefaultTreeModel

private TreePath getTreePath(TreeNode node) {
    TreeNode[] nodes = model.getPathToRoot(node);
    TreePath path = new TreePath(nodes);
    return path;
}
+1

All Articles