Link to home
Start Free TrialLog in
Avatar of rjopata
rjopata

asked on

Jtree Enumeration

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.
Avatar of girionis
girionis
Flag of Greece image

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?
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of expertmb
expertmb

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

find the node then traverse
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
Avatar of rjopata

ASKER

Thanks everyone for such useful comments and information.i guess i will have to check and get back soon.
regards.

    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);
            }
        }
    }
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.
objects,
zzynx already posted which has the code of visitAllNodes() except the if (node.isLeaf()) :)
typo,
zzynx already posted the link  which has the code of visitAllNodes() except the if (node.isLeaf())
> except the if (node.isLeaf()) :)

Was think thats what was meant by only enumerate nodes
Avatar of rjopata

ASKER

> except the if (node.isLeaf())
thats right object.
easy everyone.:)
thanks a lot.
Avatar of rjopata

ASKER

all comments were really useful.
thanks everybody.
Thanks for accepting