Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all childs of root node in jtree?

Tags:

java

swing

jtree

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..

like image 996
Babu R Avatar asked Jul 13 '12 12:07

Babu R


2 Answers

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.

like image 90
JB Nizet Avatar answered Nov 15 '22 14:11

JB Nizet


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

  • TreeNode#children
  • TreeNode#getChildAt
  • TreeNode#getChildCount

Otherwise, you can directly query the TreeModel. See the

  • TreeModel#getRoot
  • TreeModel#getChild

methods

like image 37
Robin Avatar answered Nov 15 '22 13:11

Robin