Java – how to get the depth of the current node in JTree?

I have a JTree with several nodes and children When I click on a node, I want to know its depth (0,1,3) How should I know?

selected_node.getDepth();

Do not return the depth of the current node

Solution

You should use getlevel Getlevel returns the number of levels above this node - the distance from the root node to this node If this node is the root node, 0 Or, if for some reason you get the treenode [] path (using getpath ()), it is sufficient to get the length of the array

GetDepth is different because it returns the depth of the tree rooted at this node This is not what you want

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>