Solved

How to get a node value of JTree?

Posted on 2001-08-18
6
1,042 Views
Last Modified: 2008-01-16
I want to get the values of the all the nodes of JTree one by one, how can I do this? Thanks!
0
Comment
Question by:fungho
6 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
If you use DefaultMutableTreeNode's to build your tree, then that class has methods for getting enumerator's that return all the nodes in various orders.
0
 
LVL 15

Expert Comment

by:ozymandias
Comment Utility
You start with your JTree, let's says it's called tree.

From JTree you can get the TreeModel :

    TreeModel model = tree.getModel();

From the Model you can get the root object :

    Object obj = model.getRoot();

If we assume that the root object is a DefaultMutableTreeNode we can cast it, thus :

    DefaultMutableTreeNode node = (DefaultMutableTreeNode) obj;

Now that we have the root as a node we can get an enumeration of the entire tree :

    Enumeration e = node.depthFirstEnumeration();

or

    Enumeration e = node.breadthFirstEnumeration();

Then we can use the enumeratoin to get each node :

    while (e.hasModeElements){
        ...etc
    }

Hope this helps.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Thanks for expanding on my comment :)
But remember we can't always assume the root node is a DefaultMutableTreeNode.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Accepted Solution

by:
wamoz earned 50 total points
Comment Utility
A good book on the subject is Core Swing Advanced Programming by Kim Topley.

Fungho, JTrees don't have nodes. They have TreeModels. If you don't create a TreeModel and assign it then a JTree will manufacture an instance of DefaultMutableTreeModel (a class implementing the TreeModel interface) and use that.

TreeModels have nodes. Look up the TreeModel interface to find out how to access individual nodes.

I could tell you how to access nodes but someone already has and besides, one cannot help but wonder have you so much as glanced at the documentation. There are several examples in the Java Trails, including (I seem to remember) a recursive descent treewalk.
0
 

Author Comment

by:fungho
Comment Utility
Thanks for wamoz! At the same time, I also want to thanks to ozymandias! I did not have time to try this, but I will implement this later. I also did not have enough time to study the documentation in fact. Now I understand JTree more.
0
 
LVL 1

Expert Comment

by:wamoz
Comment Utility
Re-reading my response I think I owe you an apology, because being right is no excuse for (my) bad manners.

Don't let your employer bully you, Fungho. You NEED time to study the documentation... the Java libraries, though layered, cohesive and IMHO rather well designed, are vast.

The Java Trails are a extensive and form both a very good introduction to basic material and a great way to re-familiarise oneself with the how-to of core technologies. I recommend that you download them.

They are available as either a Windows help file or as a web of linked HTML files. I prefer the HTML version. The first thing I do at any workplace is install them (along with the documentation for all the Java libraries I install) on a webserver so that everyone can have high-speed access. At home I run a webserver for this same purpose.

In respect of TreeModels, you can implement your own. To do anything even a little advanced you will almost certainly end up doing so; this allows you to add all sorts of extra properties and methods both to the tree model and to the node objects it organises.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now