How to get depth of current node in JTree?
depth, java, jtree, swing, treenode
Solution
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.
Problem
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..