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
Solved

How to get a node value of JTree?

Posted on 2001-08-18
6
1,057 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
LVL 1

Accepted Solution

by:
wamoz earned 50 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
diffSum example 4 50
Arrays.asList  VS  ArrayList 4 71
javap not working 8 58
add projects t working set in maven 2 17
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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:
The viewer will learn how to implement Singleton Design Pattern in Java.

809 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