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?
JTree delegates the display of its items to Renderers. By default, a renderer is automatically created for a JTree to display all its items. The rendering is represented by an interface called TreeCellRenderer. The Swing API provides a default implementation of this interface known as DefaultTreeCellRenderer.
JTree is a concept used in Java swing methodology. It is used to display hierarchical data which is in a particular order. It also has a root node which is the most important node in the Java framework. Also, the Jtree concept is used in programming languages wherever a hierarchy of data has to be displayed.
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
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With