如何根据字母动态搜索JTree(不是每个确切的节点名称)?

时间:2015-05-08 20:31:53

标签: java regex pattern-matching nodes jtree

我想创建一个可以通过在文本字段中键入字母来搜索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();
    }
}

1 个答案:

答案 0 :(得分:3)

将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类中更改一个,另一个是无用的