Search code examples
javastringswingjtablerowfilter

JTable filtering by an exact match to a String


I want to filter a JTable exactly by a string. My filter is like this:

Pattern.quote(textfield.getText());

But, when I filter on "G" I get also all lines of the JTable with the entry "KG". I just want the rows with the entry "G". I looked at How to Use Tables: Sorting and Filtering, but I still don't see how.


Solution

  • Another example: RowFilter#regexFilter(...) (Java Platform SE 8)

    The returned filter uses Matcher.find() to test for inclusion. To test for exact matches use the characters '^' and '$' to match the beginning and end of the string respectively. For example, "^foo$" includes only rows whose string is exactly "foo" and not, for example, "food". See Pattern for a complete description of the supported regular-expression constructs.

    import java.awt.*;
    import java.awt.event.*;
    import java.util.regex.*;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class JTableFilterDemo2 {
      public JComponent makeUI() {
        String[] columnNames = {"Item"};
        Object[][] data = {{"G"}, {"KG"}, {"XG"}, {"Y"}, {"Z"}, {"*G"}};
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        TableRowSorter<TableModel> sorter = new TableRowSorter<>(model);
        JTable table = new JTable(model);
        table.setRowSorter(sorter);
    
        JTextField textField = new JTextField("G");
    
        JButton button = new JButton("Toggle filter");
        button.addActionListener(e -> {
          if (sorter.getRowFilter() != null) {
            sorter.setRowFilter(null);
          } else {
            String text = Pattern.quote(textField.getText());
            String regex = String.format("^%s$", text);
            sorter.setRowFilter(RowFilter.regexFilter(regex));
          }
        });
    
        JPanel p = new JPanel(new BorderLayout());
        p.add(textField, BorderLayout.NORTH);
        p.add(new JScrollPane(table));
        p.add(button, BorderLayout.SOUTH);
        return p;
      }
      public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
          JFrame f = new JFrame();
          f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          f.getContentPane().add(new JTableFilterDemo2().makeUI());
          f.setSize(320, 240);
          f.setLocationRelativeTo(null);
          f.setVisible(true);
        });
      }
    }