?
Solved

How to get a node value of JTree?

Posted on 2001-08-18
6
Medium Priority
?
1,105 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
ID: 6403096
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
ID: 6403236
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
ID: 6404535
Thanks for expanding on my comment :)
But remember we can't always assume the root node is a DefaultMutableTreeNode.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Accepted Solution

by:
wamoz earned 200 total points
ID: 6410590
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
ID: 6414369
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
ID: 6414468
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

864 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