Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to recursively traverse JTree?

 
Liana Norpunova
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21318
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic