Close

Java Swing - Filtering rows in JTable

[Updated: Jul 4, 2018, Created: Jul 27, 2017]

This example shows how to use javax.swing.RowFilter to filter out table model entries so that they will not be shown in the view.

Creating the RowFilter

This is a generic util method to set a RowFilter to the JTable:

public class RowFilterUtil {
    public static JTextField createRowFilter(JTable table) {
        RowSorter<? extends TableModel> rs = table.getRowSorter();
        if (rs == null) {
            table.setAutoCreateRowSorter(true);
            rs = table.getRowSorter();
        }

        TableRowSorter<? extends TableModel> rowSorter =
                (rs instanceof TableRowSorter) ? (TableRowSorter<? extends TableModel>) rs : null;

        if (rowSorter == null) {
            throw new RuntimeException("Cannot find appropriate rowSorter: " + rs);
        }

        final JTextField tf = new JTextField(15);
        tf.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                update(e);
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                update(e);
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                update(e);
            }

            private void update(DocumentEvent e) {
                String text = tf.getText();
                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
            }
        });

        return tf;
    }
}

The main class

public class ExampleMain {
  public static void main(String[] args) {
      JFrame frame = createFrame();
      TableModel tableModel = createTableModel();
      JTable table = new JTable(tableModel);

      JTextField filterField = RowFilterUtil.createRowFilter(table);
      JPanel jp = new JPanel();
      jp.add(filterField);
      frame.add(jp, BorderLayout.NORTH);

      JScrollPane pane = new JScrollPane(table);
      frame.add(pane, BorderLayout.CENTER);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
  }

  private static TableModel createTableModel() {
      Vector<String> columns = new Vector<>(Arrays.asList("Name", "Address", "Age"));
      Vector<Vector<Object>> rows = new Vector<>();

      DataFactory dataFactory = new DataFactory();
      for (int i = 1; i <= 30; i++) {
          Vector<Object> v = new Vector<>();
          v.add(dataFactory.getName());
          v.add(dataFactory.getAddress() + ", " + dataFactory.getCity());
          v.add(dataFactory.getNumberBetween(18, 80));
          rows.add(v);
      }

      DefaultTableModel dtm = new DefaultTableModel(rows, columns) {
          @Override
          public Class<?> getColumnClass(int columnIndex) {
              return columnIndex == 2 ? Integer.class : String.class;
          }
      };
      return dtm;
  }

  private static JFrame createFrame() {
      JFrame frame = new JFrame("JTable Row filter example");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(new Dimension(600, 450));
      return frame;
  }
}

Output

Example Project

Dependencies and Technologies Used:

  • datafactory 0.8: Library to generate data for testing.
  • JDK 1.8
  • Maven 3.3.9

Swing Jtable Row Filter Example Select All Download
  • table-row-filter
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • RowFilterUtil.java

    See Also