过滤 jtree - 保留所有节点和 children 个符合条件的节点

Filter jtree - keeping all nodes and children of nodes that match criteria

我有一个 DefaultMutableTreeNodes 的 JTree,我想过滤它们。

当我进行过滤时,我想保留任何符合我的标准或具有 children 符合我的标准的节点。

这里放上代码供大家参考。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;

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.border.EmptyBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeModel;

public class FilteredJTreeExample extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTextField textField;
    private JTree tree;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    FilteredJTreeExample frame = new FilteredJTreeExample();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public FilteredJTreeExample() {
        //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.NORTH);
        GridBagLayout gbl_panel = new GridBagLayout();
        gbl_panel.columnWidths = new int[]{34, 116, 0};
        gbl_panel.rowHeights = new int[]{22, 0};
        gbl_panel.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
        gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
        panel.setLayout(gbl_panel);

        JLabel lblFilter = new JLabel("Search:");
        GridBagConstraints gbc_lblFilter = new GridBagConstraints();
        gbc_lblFilter.anchor = GridBagConstraints.WEST;
        gbc_lblFilter.insets = new Insets(0, 0, 0, 5);
        gbc_lblFilter.gridx = 0;
        gbc_lblFilter.gridy = 0;
        panel.add(lblFilter, gbc_lblFilter);

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

        tree = new JTree();
        tree.setEditable( true );
        tree.setShowsRootHandles( false );
        tree.setInvokesStopCellEditing(true);
        scrollPane.setViewportView(tree);

        textField = new JTextField();
        GridBagConstraints gbc_textField = new GridBagConstraints();
        gbc_textField.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField.anchor = GridBagConstraints.NORTH;
        gbc_textField.gridx = 1;
        gbc_textField.gridy = 0;
        panel.add(textField, gbc_textField);
        textField.setColumns(10);
        textField.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                TreeModel model = tree.getModel();
                tree.setModel(null);
                tree.setModel(model);
            }
        });

        tree.setCellRenderer(new DefaultTreeCellRenderer() {
            private JLabel lblNull = new JLabel();

            @Override
            public Component getTreeCellRendererComponent(JTree tree, Object value,
                    boolean arg2, boolean arg3, boolean arg4, int arg5, boolean arg6) {

                Component c = super.getTreeCellRendererComponent(tree, value, arg2, arg3, arg4, arg5, arg6);

                DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
                if (matchesFilter(node)) {
                    c.setForeground(Color.BLACK);
                    return c;
                }
                else if (containsMatchingChild(node)) {
                    c.setForeground(Color.GRAY);
                    return c;
                }
                else {
                    return lblNull;
                }
            }

            private boolean matchesFilter(DefaultMutableTreeNode node) {
                return node.toString().contains(textField.getText());
            }

            private boolean containsMatchingChild(DefaultMutableTreeNode node) {
                Enumeration<DefaultMutableTreeNode> e = node.breadthFirstEnumeration();
                boolean isMatched = false;
                while (e.hasMoreElements()) {
                    DefaultMutableTreeNode nextElement = e.nextElement(); 
                    if (matchesFilter(nextElement)) {
                        isMatched = true;
                    }
                }
                return isMatched;
            }
        });
    }
}

输出:

当我输入搜索文本 "color" 并单击输入时。它仅显示 JTree 和 Colors 节点。

问题:

  1. 我想显示 children 的颜色。即蓝色、紫色等,

  2. 我发现的另一个问题是在执行搜索操作后,鼠标向上滚动不起作用,而向下滚动正常。可以通过搜索 "e" 并缩小框架并上下滚动鼠标来重现。

注意:我正在使用 Ubuntu 14.04 LTS。

有什么想法吗?

您还需要一个包含节点的方法,当它的父节点(祖先节点)之一包含所需的字符串时。例如,

private boolean containsMatchingParent(DefaultMutableTreeNode node) 
{
    DefaultMutableTreeNode current = node;
    while (current != null)
    {
        if (matchesFilter(current))
        {
            return true;
        }
        current = (DefaultMutableTreeNode) current.getParent();
    }
    return false;
}

(用法同containsMatchingChild).

matchesFilter() 更改为:

private boolean matchesFilter(DefaultMutableTreeNode node) 
{
    TreeNode parent = node;
    while ( parent != null )
    {
        if ( parent.toString().contains(textField.getText()))
        {
            return true;
        }
        parent = parent.getParent();

    }

    return false;
}

这将 return 颜色,因此您将能够展开 colors 节点。