How to get all childs of root node in jtree?
Asked Answered
H

2

10

I want to get all child nodes of root node.

ex:

Root
   child1
      child1.child1
   child2
      child2.child1

Now I want to get the two child nodes called "child1" and "child2".

How to do that? Is there any possibilities?

Please help me, Thanks in advance..

Horsewhip answered 13/7, 2012 at 12:32 Comment(0)
P
19

Get the root of the tree:

 tree.getModel().getRoot();

then get the number of children of this root node:

 tree.getModel().getChildCount(rootNode)

then go from 0 to the number of children and call

tree.getModel().getChild(rootNode, i)

to get the children of the root node.

Profanatory answered 13/7, 2012 at 12:40 Comment(3)
Thanks i got the answer. pls tell me how to remove all child nodes of that root node.Horsewhip
you forgot the parameter in the second :-)Henke
But this doesn't check children inside children ?Viceregent
P
6

If your elements implement the TreeNode interface, you can use the available methods:

Otherwise, you can directly query the TreeModel. See the

methods

Pullen answered 13/7, 2012 at 12:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.