jQuery in Action, 3rd edition
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
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, www.gsi3d.org.uk

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: http://aspose.com/file-tools
subject: JTree traversal
It's not a secret anymore!