Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes How to recursively traverse JTree? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to recursively traverse JTree?" Watch "How to recursively traverse JTree?" New topic
Author

How to recursively traverse JTree?

Liana Norpunova
Greenhorn

Joined: Aug 24, 2010
Posts: 23
Hi!

There is JTree component that I want to fill from the database. New nodes can be dynamically added to this tree. I need a procedure that will recursively traverse a tree. My problem is that only second-level nodes are added to the root node, but other-level nodes are not created. I've included System.out.println to see the results and problems. Please, help me to fix the error. Below is my code:


The output produced by System.out.println:

Thanks a lot!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I would just call either the breadthFirstEnumeration() or the depthFirstEnumeration() method of the DefaultMutableTreeNode which is the root of the tree, if I wanted to traverse all the nodes of the tree model. I wouldn't bother to write my own code.

But what I actually do is to have the user object contain a link to the node which contains it. That way there's no searching required at all.
Liana Norpunova
Greenhorn

Joined: Aug 24, 2010
Posts: 23
Well, probably my question is a bit unclear. I want to ADD nodes to the JTree, and not only search them.

Initially, I have a data set stored in the database and a BLANK JTree. This data set contains a tree structure, e.g.:
1. Documents
1.1 Correspondence
1.1.1 Incoming
1.1.2 Outgoing
1.2 Contracts
etc.

What I need, is to build the JTree based on the above-shown information.

So, does the depthFirstEnumeration() method of the DefaultMutableTreeNode provides possibilities to ADD nodes?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to recursively traverse JTree?
 
Similar Threads
Jtree adding nodes
zip to JTree
JCheckBox in Jtree Problem
Jtree - message becomes blurry when scrolls
JTree refresh, with new TreeModel