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..
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With