Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get depth of current node in JTree?

I have a JTree with a few nodes and subnodes. When I click on a node I want to know on which depth is it (0, 1, 3). How can I know that?

selected_node.getDepth(); 

doesn't return the depth of current node..

like image 465
c0dehunter Avatar asked Apr 10 '12 12:04

c0dehunter


1 Answers

You should be using getLevel. getLevel returns the number of levels above this node -- the distance from the root to this node. If this node is the root, returns 0. Alternatively, if for whatever reason you have obtained the Treenode[] path (using getPath()) then it is sufficient to take the length of that array.

getDepth is different, as it returns the depth of the tree rooted at this node. Which is not what you want.

like image 56
Anonymous Avatar answered Sep 23 '22 18:09

Anonymous