如何按字母动态搜索 JTree(而不是按确切的节点名称)?

How to search a JTree dynamically per letter (not per exact node name)?

我想制作一个可以通过在文本字段中键入字母来搜索 JTree 的应用程序。我希望它显示与不完整输入相匹配的单词。就像它应该匹配节点 "Sample" 即使用户只输入了 "Sa"。

这是我的代码。它正在工作,但仅适用于整个单词。谢谢!

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

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

public class SearchTree extends JFrame {

    private DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");

    private DefaultTreeModel model = new DefaultTreeModel(root);

    private JTree tree = new JTree(model);

    private JLabel searchLabel = new JLabel("Enter Node to be searched");

    private JTextField searchText;

    public SearchTree() {
        DefaultMutableTreeNode n1 = new DefaultMutableTreeNode("Sample 1");
        n1.add(new DefaultMutableTreeNode("2nd level: Child l"));
        n1.add(new DefaultMutableTreeNode("Search Me"));
        n1.add(new DefaultMutableTreeNode("Bob"));

        DefaultMutableTreeNode n2 = new DefaultMutableTreeNode("Find me");
        n2.add(new DefaultMutableTreeNode("2nd level: Child 2"));
        n2.add(new DefaultMutableTreeNode("Peter"));
        n2.add(new DefaultMutableTreeNode("Lily"));

        DefaultMutableTreeNode n3 = new DefaultMutableTreeNode("Explore me");
        n3.add(new DefaultMutableTreeNode("Search Me Please"));
        n3.add(new DefaultMutableTreeNode("Rome"));
        n3.add(new DefaultMutableTreeNode("Italy"));

        root.add(n1);
        root.add(n2);
        root.add(n3);

        tree.setEditable(true);
        tree.setSelectionRow(0);

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

        JPanel searchPanel = new JPanel();
        searchPanel.setBorder(BorderFactory.createEtchedBorder());

        searchText = new JTextField(20);
        searchText.getDocument().addDocumentListener(new SearchListener());
        searchPanel.add(searchText);


        searchPanel.add(searchLabel);

        getContentPane().add(searchPanel, BorderLayout.SOUTH);
        setSize(700, 400);
        setVisible(true);

    }


    public void removeNode(DefaultMutableTreeNode selNode) {
        if (selNode == null) {
            return;
        }
        MutableTreeNode parent = (MutableTreeNode) (selNode.getParent());
        if (parent == null) {
            return;
        }
        MutableTreeNode toBeSelNode = getSibling(selNode);
        if (toBeSelNode == null) {
            toBeSelNode = parent;
        }
        TreeNode[] nodes = model.getPathToRoot(toBeSelNode);
        TreePath path = new TreePath(nodes);
        tree.scrollPathToVisible(path);
        tree.setSelectionPath(path);
        model.removeNodeFromParent(selNode);
    }

    private MutableTreeNode getSibling(DefaultMutableTreeNode selNode) {
        MutableTreeNode sibling = (MutableTreeNode) selNode
                .getPreviousSibling();
        if (sibling == null) {
            sibling = (MutableTreeNode) selNode.getNextSibling();
        }
        return sibling;
    }

    private class SearchListener implements DocumentListener{

        @Override
        public void insertUpdate(DocumentEvent e) {
            DefaultMutableTreeNode node = searchNode(searchText.getText());
            if (node != null) {
                TreeNode[] nodes =model.getPathToRoot(node);
                TreePath path = new TreePath(nodes);
                tree.scrollPathToVisible(path);
                tree.setSelectionPath(path);
            } else {
                System.out.println("Node with string "
                        + searchText.getText() + " not found");
            }

        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            DefaultMutableTreeNode node = searchNode(searchText.getText());
            if (node != null) {
                TreeNode[] nodes =model.getPathToRoot(node);
                TreePath path = new TreePath(nodes);
                tree.scrollPathToVisible(path);
                tree.setSelectionPath(path);
            } else {
                System.out.println("Node with string "
                        + searchText.getText() + " not found");
            }
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            DefaultMutableTreeNode node = searchNode(searchText.getText());
            if (node != null) {
                TreeNode[] nodes =model.getPathToRoot(node);
                TreePath path = new TreePath(nodes);
                tree.scrollPathToVisible(path);
                tree.setSelectionPath(path);
            } else {
                System.out.println("Node with string "
                        + searchText.getText() + " not found");
            }
        }

        public DefaultMutableTreeNode searchNode(String nodeStr) {
            DefaultMutableTreeNode node = null;
            Enumeration e = root.breadthFirstEnumeration();
            while (e.hasMoreElements()) {
                node = (DefaultMutableTreeNode) e.nextElement();
                if (nodeStr.equals(node.getUserObject().toString())) {
                    return node;
                }
            }
            return null;
        }
    }

    public static void main(String[] arg) {
        SearchTree st = new SearchTree();
    }
}

将您的 searchNode 函数更改为:

public DefaultMutableTreeNode searchNode(String nodeStr) {
    Enumeration e = root.breadthFirstEnumeration();
    while (e.hasMoreElements()) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
        if (node.getUserObject().toString().toLowerCase().startsWith(nodeStr.toLowerCase())) {
            return node;
        }
    }
    return null;
}

不使用 .equals(" ") 函数,而是使用 .startsWith(" ")

编辑: 您有 2 个 searchNode 函数,请确保在 SearchListener class 中更改一个,另一个没用