Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to get a node value of JTree?

Posted on 2001-08-18
6
Medium Priority
?
1,097 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

636 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