我有一个JTree,并且在JTree中有一个节点,但是该节点的深度大约为5到6级。我有一个通往节点的路径。我想展开JTree以显示该节点。因此我尝试了myJTree.expandPath(myJTree.getPath(myNode));但它不起作用,它仅扩展了myJTree的第一级。我能做什么?我已验证路径有效。

我有该节点的路径,并且尝试使用myJTree.expandPath(myJTree.getPath(myNode))进行扩展,但实际上不起作用。

private TreePath getPath(TreeNode treeNode) {
    List<Object> nodes = new ArrayList<Object>();
    if (treeNode != null) {
        nodes.add(treeNode);
        treeNode = treeNode.getParent();
        while (treeNode != null) {
            nodes.add(0, treeNode);
            treeNode = treeNode.getParent();
        }
    }
    return nodes.isEmpty() ? null : new TreePath(nodes.toArray());
}

最佳答案

试试这个例子...它允许您按名称搜索节点,并将扩展到找到的第一个节点

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.Enumeration;
    import java.util.List;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTree;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeModel;
    import javax.swing.tree.TreePath;

    public class TreeDemo extends JFrame implements ActionListener{
        private static final long serialVersionUID = 1L;
        public JTree tree;
        public JButton button;

        public TreeDemo() {

            button = new JButton("Find Queen");


            button.addActionListener(this);

            tree = new JTree();
            DefaultMutableTreeNode root = new DefaultMutableTreeNode( "Deck" );
            DefaultMutableTreeNode itemClubs= new DefaultMutableTreeNode( "Clubs" );
            addAllCard( itemClubs );
            root.add( itemClubs );

            DefaultMutableTreeNode itemDiamonds = new DefaultMutableTreeNode( "Diamonds" );
            addAllCard( itemDiamonds );
            root.add( itemDiamonds );

            DefaultMutableTreeNode itemSpades = new DefaultMutableTreeNode( "Spades" );
            addAllCard( itemSpades );
            root.add( itemSpades );

            DefaultMutableTreeNode itemHearts = new DefaultMutableTreeNode( "Hearts" );
            addAllCard( itemHearts );
            root.add( itemHearts );

            DefaultTreeModel treeModel = new DefaultTreeModel( root );
            tree = new JTree( treeModel );

            JScrollPane scrollPane = new JScrollPane(tree);
            getContentPane().add(scrollPane, BorderLayout.CENTER);
            getContentPane().add(button, BorderLayout.NORTH);

            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(375, 400);
        }

        public void addAllCard( DefaultMutableTreeNode suit ) {
            suit.add( new DefaultMutableTreeNode( "Ace" ) );
            suit.add( new DefaultMutableTreeNode( "Two" ) );
            suit.add( new DefaultMutableTreeNode( "Three" ) );
            suit.add( new DefaultMutableTreeNode( "Four" ) );
            suit.add( new DefaultMutableTreeNode( "Five" ) );
            suit.add( new DefaultMutableTreeNode( "Six" ) );
            suit.add( new DefaultMutableTreeNode( "Seven" ) );
            suit.add( new DefaultMutableTreeNode( "Eight" ) );
            suit.add( new DefaultMutableTreeNode( "Nine" ) );
            suit.add( new DefaultMutableTreeNode( "Ten" ) );
            suit.add( new DefaultMutableTreeNode( "Jack" ) );
            suit.add( new DefaultMutableTreeNode( "Queen" ) );
            suit.add( new DefaultMutableTreeNode( "King" ) );
        }

        public final DefaultMutableTreeNode findNode(String searchString) {

            List<DefaultMutableTreeNode> searchNodes = getSearchNodes((DefaultMutableTreeNode)tree.getModel().getRoot());
            DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();

            DefaultMutableTreeNode foundNode = null;
            int bookmark = -1;

            if( currentNode != null ) {
                for(int index = 0; index < searchNodes.size(); index++) {
                    if( searchNodes.get(index) == currentNode ) {
                        bookmark = index;
                        break;
                    }
                }
            }

            for(int index = bookmark + 1; index < searchNodes.size(); index++) {
                if(searchNodes.get(index).toString().toLowerCase().contains(searchString.toLowerCase())) {
                    foundNode = searchNodes.get(index);
                    break;
                }
            }

            if( foundNode == null ) {
                for(int index = 0; index <= bookmark; index++) {
                    if(searchNodes.get(index).toString().toLowerCase().contains(searchString.toLowerCase())) {
                        foundNode = searchNodes.get(index);
                        break;
                    }
                }
            }
            return foundNode;
        }

        private final List<DefaultMutableTreeNode> getSearchNodes(DefaultMutableTreeNode root) {
            List<DefaultMutableTreeNode> searchNodes = new ArrayList<DefaultMutableTreeNode>();

            Enumeration<?> e = root.preorderEnumeration();
            while(e.hasMoreElements()) {
                searchNodes.add((DefaultMutableTreeNode)e.nextElement());
            }
            return searchNodes;
        }

        public static void main(String[] args) {
            TreeDemo app = new TreeDemo();
            app.setVisible(true);


        }


        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode node = findNode("Queen");
            if( node != null ) {
                TreePath path = new TreePath(node.getPath());
                tree.setSelectionPath(path);
                tree.scrollPathToVisible(path);
            }
        }
    }

10-05 18:41