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?
Asked
Active
Viewed 1.8k times
3 Answers
14
You should be able to call getLastPathComponent
on the TreePath
and cast that for a TreeNode
or DefaultMutableTreeNode
and be good to go.

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