• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to recursively traverse JTree?

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 28360
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
This looks like a job for .... legal tender! It says so right in this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic