Java tutorial
import java.awt.BorderLayout; import java.awt.Container; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.RowFilter; import javax.swing.table.AbstractTableModel; import javax.swing.table.TableRowSorter; class SimpleTableModel extends AbstractTableModel { private Object[][] data = {}; private String[] columnNames = { "ID", "Name", "Gender" }; private Class[] columnClass = { Integer.class, String.class, String.class }; private Object[][] rowData = new Object[][] { { new Integer(1), "Tom", "Male" }, { new Integer(2), "Jack", "Female" } }; public SimpleTableModel() { } @Override public int getRowCount() { return rowData.length; } @Override public int getColumnCount() { return columnNames.length; } @Override public String getColumnName(int columnIndex) { return columnNames[columnIndex]; } @Override public Class getColumnClass(int columnIndex) { return columnClass[columnIndex]; } @Override public boolean isCellEditable(int rowIndex, int columnIndex) { boolean isEditable = true; if (columnIndex == 0) { isEditable = false; // Make the ID column non-editable } return isEditable; } @Override public Object getValueAt(int rowIndex, int columnIndex) { return rowData[rowIndex][columnIndex]; } @Override public void setValueAt(Object aValue, int rowIndex, int columnIndex) { rowData[rowIndex][columnIndex] = aValue; } } public class Main extends JFrame { public Main() { this.setDefaultCloseOperation(EXIT_ON_CLOSE); JTable table = new JTable(new SimpleTableModel()); TableRowSorter sorter = new TableRowSorter(table.getModel()); table.setRowSorter(sorter); RowFilter<SimpleTableModel, Integer> IDFilter = new RowFilter<SimpleTableModel, Integer>() { @Override public boolean include(Entry<? extends SimpleTableModel, ? extends Integer> entry) { SimpleTableModel model = entry.getModel(); int rowIndex = entry.getIdentifier().intValue(); Integer ID = (Integer) model.getValueAt(rowIndex, 0); if (ID.intValue() <= 100) { return false; // Do not show rows with an ID <= 100 } return true; } }; sorter.setRowFilter(IDFilter); Container contentPane = this.getContentPane(); contentPane.add(new JScrollPane(table), BorderLayout.CENTER); } public static void main(String[] args) { Main bf = new Main(); bf.pack(); bf.setVisible(true); } }