This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JTree traversal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTree traversal" Watch "JTree traversal" New topic

JTree traversal

Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342
I have a JTree, each node of which contains an object of my own invention called 'Effect'. I need to traverse this tree using the preorder method and access the data at each node as I go.
I have defined the below methods to handle the traversal and test the result by outputting something. This works fine, but the enumeration only holds the result of the Effect toString() method - so how can I use rootNode.preorderEnumeration, and then access the variables of the actual Effect data object stored in each node? Any ideas? Help is greatly appreciated

SCJP 1.4,

Joined: Jan 10, 2002
Posts: 5
try this
since the return type of e.nextElement() is Object and you know it is a DefaultMutableTreeNode. Using the getUserObject() will return you a Effect-type object
Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342
Thanks for the suggestion. That works. I also figured out that my root node, which I neither display nor use within the application, does not contain an Effect user object but contains a string user object - this was the 'root' cause of the problem ;-)
thanks for the help,
[ January 14, 2002: Message edited by: Ben Wood ]
I agree. Here's the link:
subject: JTree traversal
Similar Threads
XML to DOM to JTree
Getting the path of file selected in JTree item
Need help on JTree