8

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?

user489041
  • 27,916
  • 55
  • 135
  • 204

3 Answers3

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

dontocsata
  • 3,021
  • 3
  • 20
  • 19
7

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

Howard
  • 38,639
  • 9
  • 64
  • 83
2

model is a DefaultTreeModel

private TreePath getTreePath(TreeNode node) {
    TreeNode[] nodes = model.getPathToRoot(node);
    TreePath path = new TreePath(nodes);
    return path;
}
Alobes5
  • 463
  • 5
  • 8