?
Solved

How to get a node value of JTree?

Posted on 2001-08-18
6
Medium Priority
?
1,085 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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
Course of the Month12 days, 9 hours left to enroll

777 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