通过与String的精确匹配进行JTable过滤

时间:2016-05-09 08:52:07

标签: java string swing jtable rowfilter

我想用字符串完全过滤JTable。我的过滤器是这样的:

Pattern.quote(textfield.getText());

但是,当我过滤“G”时,我也会得到JTable的所有行,条目为“KG”。我只想要带有条目“G”的行。我查看How to Use Tables: Sorting and Filtering,但我仍然不知道如何。

2 个答案:

答案 0 :(得分:3)

How to Use Tables: Sorting and Filtering所示,使用RowFilter来过滤掉模型中的条目,以便它们不会显示在视图中。“要在列0中完全匹配“G”,您的过滤器可能如下所示:

final RowFilter<TableModel, Integer> filter = new RowFilter<TableModel, Integer>() {
    @Override
    public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry) {
        return "G".equals(entry.getStringValue(0));
    }
};

根据此example,按下示例中的按钮切换过滤器。

image

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

/** https://stackoverflow.com/a/37114447/230513 */
public class JTableFilterDemo {

    private static TableRowSorter<TableModel> sorter;
    private Object[][] data = {{"G"}, {"KG"}, {"XG"}, {"Y"}, {"Z"}};
    private String columnNames[] = {"Item"};
    private TableModel model = new DefaultTableModel(data, columnNames) {
        @Override
        public Class<?> getColumnClass(int column) {
            return String.class;
        }
    };
    private JTable table = new JTable(model);

    public JTableFilterDemo() {
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        sorter = new TableRowSorter<>(model);
        table.setRowSorter(sorter);
        final RowFilter<TableModel, Integer> filter = new RowFilter<TableModel, Integer>() {
            @Override
            public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry) {
                return "G".equals(entry.getStringValue(0));
            }
        };
        JScrollPane scrollPane = new JScrollPane(table);
        JFrame frame = new JFrame("Filtering Table");
        frame.add(new JButton(new AbstractAction("Toggle filter") {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (sorter.getRowFilter() != null) {
                    sorter.setRowFilter(null);
                } else {
                    sorter.setRowFilter(filter);
                }
            }
        }), BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(scrollPane);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JTableFilterDemo jtfd = new JTableFilterDemo();
            }
        });
    }
}

答案 1 :(得分:2)

另一个例子:RowFilter#regexFilter(...) (Java Platform SE 8)

  

返回的过滤器使用Matcher.find()来测试是否包含。要测试完全匹配,请使用字符&#39; ^&#39;和&#39; $&#39;分别匹配字符串的开头和结尾。例如,&#34; ^ foo $&#34;仅包括其字符串正好为&#34; foo&#34;而不是,例如,&#34;食物&#34;。有关受支持的正则表达式构造的完整描述,请参阅Pattern。

nsIDOMChromeWindow