aspose file tools*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTree traversal" Watch "JTree traversal" New topic
Author

JTree traversal

Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342
Hello,
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
regards,
Ben.




SCJP 1.4, www.gsi3d.org.uk
ping203
Greenhorn

Joined: Jan 10, 2002
Posts: 5
try this
((DefaultMutableTreeNode)e.nextElement()).getUserObject()
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,
Ben.
[ January 14, 2002: Message edited by: Ben Wood ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTree traversal
 
Similar Threads
JTree
Need help on JTree
JTree
XML to DOM to JTree
Getting the path of file selected in JTree item