How can we filter a JTable in Java?

AWTJavaObject Oriented ProgrammingProgrammingSwing

A JTable provides a very flexible possibility to create and display tables. The TableModel interface defines methods for objects that specify the contents of a table. The AbstractTableModel class is typically extended to provide a custom implementation of a model table. A JTable class provides the ability to edit tables using the method setCellEditor() allows an object of the TableCellEditor interface.

We can filter a table using the setRowFilter() method of TableRowSorter class.

Example

import java.awt.*;
import java.awt.event.*;
import java.util.regex.*;
import javax.swing.*;
import javax.swing.table.*;
public class FilterTableTest extends JFrame {
   private JTable table;
   private TableModel model;
   public FilterTableTest() {
      setTitle("FilterTable Test");
      Object rows[][] = {{"Adithya", "Content Developer", 25000}, {"Jai", "SME", 30000},  {"Chaitanya", "Java Engineer", 45000}, {"Ramesh", "Scala Developer", 40000}, {"Ravi", "SAP  Consultant", 70000}};
      Object columns[] = {"Name", "Designation", "Salary"};
      model = new DefaultTableModel(rows, columns) {
         public Class getColumnClass(int column) {
            Class returnValue;
            if((column >= 0) && (column < getColumnCount())) {
               returnValue = getValueAt(0, column).getClass();
            } else {
               returnValue = Object.class;
            }
            return returnValue;
         }
      };
      table = new JTable(model);
      final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
      table.setRowSorter(sorter);
      add(new JScrollPane(table), BorderLayout.CENTER);
      JPanel panel = new JPanel(new BorderLayout());
      JLabel label = new JLabel("Filter");
      panel.add(label, BorderLayout.WEST);
      final JTextField filterText = new JTextField("");
      panel.add(filterText, BorderLayout.CENTER);
      add(panel, BorderLayout.NORTH);
      JButton button = new JButton("Filter");
      button.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            String text = filterText.getText();
            if(text.length() == 0) {
               sorter.setRowFilter(null);
            } else {
               try {
                  sorter.setRowFilter(RowFilter.regexFilter(text));
               } catch(PatternSyntaxException pse) {
                     System.out.println("Bad regex pattern");
               }
             }
         }
      });
      add(button, BorderLayout.SOUTH);
      setSize(400, 300);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setVisible(true);
   }
   public static void main(String args[]) {
      new FilterTableTest();
   }
}

Output


raja
Published on 03-Sep-2019 15:12:51
Advertisements