简体   繁体   English

如何获取JTree中当前节点的深度?

[英]How to get depth of current node in JTree?

I have a JTree with a few nodes and subnodes.我有一个带有几个节点和子节点的 JTree。 When I click on a node I want to know on which depth is it (0, 1, 3).当我点击一个节点时,我想知道它在哪个深度(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 getLevel returns the number of levels above this node -- the distance from the root to this node. getLevel返回此节点上方的层数——从根到此节点的距离。 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.如果此节点是根,则返回 0。或者,如果出于某种原因您已经获得了Treenode[]路径(使用getPath() ),那么获取该数组的长度就足够了。

getDepth is different, as it returns the depth of the tree rooted at this node. getDepth不同,因为它返回以该节点为根的树的深度。 Which is not what you want.这不是你想要的。

basicaly you have to Iterate inside JTree , but TreeSelectionListener can returns interesting value, for example基本上你必须在JTree内部Iterate ,但是TreeSelectionListener可以返回有趣的值,例如

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;

public class TreeSelectionRow {

    public TreeSelectionRow() {
        JTree tree = new JTree();
        TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {

            @Override
            public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
                JTree treeSource = (JTree) treeSelectionEvent.getSource();
                System.out.println("Min: " + treeSource.getMinSelectionRow());
                System.out.println("Max: " + treeSource.getMaxSelectionRow());
                System.out.println("Lead: " + treeSource.getLeadSelectionRow());
                System.out.println("Row: " + treeSource.getSelectionRows()[0]);
            }
        };
        tree.addTreeSelectionListener(treeSelectionListener);
        String title = "JTree Sample";
        JFrame frame = new JFrame(title);
        frame.add(new JScrollPane(tree));
        frame.setSize(300, 150);
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                TreeSelectionRow treeSelectionRow = new TreeSelectionRow();
            }
        });
    }
}

If you have a TreeSelectionListener which handles the TreeSelectionEvent , you can use the TreeSelectionEvent#getPaths method to retrieve the selected TreePath s.如果您有处理TreeSelectionListenerTreeSelectionEvent ,则可以使用TreeSelectionEvent#getPaths方法来检索选定的TreePath The TreePath#getPathCount method returns the depth of the selected path. TreePath#getPathCount方法返回所选路径的深度。

You can also ask it directly to the JTree (although you will need the listener to be informed when the selection changes) by using the JTree#getSelectionPaths method.您也可以使用JTree#getSelectionPaths方法直接向JTree询问(尽管您需要在选择更改时通知侦听器)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM