Filtering rows in JTable
I have manually written a java program which takes a database query from user and shows the desired rows in a JTable. User can also give a string in a textfield to filter the rows. Query works fine. But problem is with filtering. Here, I have only given the lines of code related with filtering:
Filtering mechanism works fine. But when there is no row that match with the txt, one row (title row, which contains the column names) is shown. But I don't want to show the 'title row'. How can I do it? I also want to show a message dialog containing the message "No matching row found". But return type of setRowFilter() is void. How to determine that there is no matching row ?
JTable myTable= new JTable((TableModel) fc);
final JTextField filterText = new JTextField();
JButton filterButton = new JButton ("Apply Filter");
final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel> ((TableModel) fc);
/*Here fc is the object of a class where I have overridden getColumnCount(), getRowCount(),
getValueAt() and getColumnName() methods of interface TableModel. object of a class where
I have overridden getColumnCount(), getRowCount(), getValueAt() and getColumnName() methods
of interface TableModel.*/
//Event handling code when the "Apply Filter" button is clicked
public void actionPerformed(ActionEvent e)
String txt = filterText.getText();
sorter.setRowFilter(RowFilter.regexFilter("(?i)" + txt));
I searched the web but didn't find the desired solution. Please help. Thanks.