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?
Getting a node in JTree
You should be able to call getLastPathComponent
on the TreePath
and cast that for a TreeNode
or DefaultMutableTreeNode
and be good to go.
If your treemodel consists of DefaultMutableTreeNodes you may just use
node=(DefaultMutableTreeNode)path.getLastPathComponent();
model is a DefaultTreeModel
private TreePath getTreePath(TreeNode node) {
TreeNode[] nodes = model.getPathToRoot(node);
TreePath path = new TreePath(nodes);
return path;
}
© 2022 - 2024 — McMap. All rights reserved.