Solved

Jtree Enumeration

Posted on 2004-09-13
13
664 Views
Last Modified: 2012-05-05
i have a Jtree of 3 levels (root and three other nodes).
how can i enumerate only nodes and not the entire tree.ie enumerating only interested nodes.
thanks.
0
Comment
Question by:rjopata
[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
  • 3
  • 3
  • 3
  • +3
13 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 12051840
I am afraid you will have to enumerate through all of them and select the ones you want. What exactly are you trying to do?
0
 
LVL 37

Accepted Solution

by:
zzynx earned 50 total points
ID: 12051843
You traverse the whole tree (see http://javaalmanac.com/egs/javax.swing.tree/GetNodes.html) and
pick out the nodes that you're interested in
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12051847
have  a look at here
http://javaalmanac.com/egs/javax.swing.tree/FindNode.html?l=rel

find the node then traverse
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 18

Expert Comment

by:armoghan
ID: 12051880
What yuo can do is, while inserting the nodes, you can place them in some collection, ArrayList or HashTable and use them afterwards,
Otherwise the only solution is suggested by the experts above of iterating and finding the node
0
 

Author Comment

by:rjopata
ID: 12051908
Thanks everyone for such useful comments and information.i guess i will have to check and get back soon.
regards.
0
 
LVL 92

Expert Comment

by:objects
ID: 12051912

    public void visitAllNodes(TreeNode node) {
        // node is visited exactly once
        if (node.isLeaf()) {

           // a leaf node, do as you require

        }  else if (node.getChildCount() >= 0) {
            for (Enumeration e=node.children(); e.hasMoreElements(); ) {
                TreeNode n = (TreeNode)e.nextElement();
                visitAllNodes(n);
            }
        }
    }
0
 
LVL 92

Expert Comment

by:objects
ID: 12051919
If you're using DefaultMutableTreeNode then it has methods for returning Enumerations for all nodes which you can use to enumerate thru to find the leaf nodes.
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12051935
objects,
zzynx already posted which has the code of visitAllNodes() except the if (node.isLeaf()) :)
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12051940
typo,
zzynx already posted the link  which has the code of visitAllNodes() except the if (node.isLeaf())
0
 
LVL 92

Expert Comment

by:objects
ID: 12051956
> except the if (node.isLeaf()) :)

Was think thats what was meant by only enumerate nodes
0
 

Author Comment

by:rjopata
ID: 12052050
> except the if (node.isLeaf())
thats right object.
easy everyone.:)
thanks a lot.
0
 

Author Comment

by:rjopata
ID: 12057979
all comments were really useful.
thanks everybody.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12061775
Thanks for accepting
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

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 …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

732 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