Solved

Jtree Enumeration

Posted on 2004-09-13
13
651 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
  • 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now