I have built a tree using JTree and I want to obtain the node details(like they appear in the frame drawn here).
A:
TreeNode root = (TreeNode)tree.getModel().getRoot();
Enumeration childrenEnum = root.children();
while (childrenEnum.hasMoreElements()) {
TreeNode childNode = (TreeNode)childrenEnum.nextElement();
String childString = childNode.toString();
// ....
}
Check out the docs for TreeNode (the 'highest level' interface) and DefaultMutableTreeNode (the most commonly used implementation). DefaultMutableTreeNode offers the getUserObject() / setUserObject() methods if you need to hold more complex data in the tree node.
Nate
2009-09-25 13:00:12