I have created JTable row filter, which is working well and filtering rows according to typing in JTextfield, but it filters according to typed character present anywhere in the row whereas I want to filter the row starting with the typed character. Is there any regex flag for it?
My table row filter code:
public static void setFilter(JTable table,String value) {
sorter = new TableRowSorter<>((DefaultTableModel)table.getModel());
table.setRowSorter(sorter);
RowFilter<DefaultTableModel, Object> rf = null;
try {
rf = RowFilter.regexFilter("(?i)" + value, columnIndex); //("(?i)" for case insensitive filter
} catch (java.util.regex.PatternSyntaxException e) {
return;
}
sorter.setRowFilter(rf);
}
rf = RowFilter.regexFilter("(?i)" + value, columnIndex);
but it filters according to typed character present anywhere in the row
It filters based on the data found in the column specified by columnIndex.
I want to filter the row starting with the typed character.
If you are saying you want to filter based on matching from the first character of the data found in the specified column then you should be able to use:
rf = RowFilter.regexFilter("^" + value, columnIndex);
Read the API for the Pattern
class. The Boundary Matchers
section shows that the "^" is used to match characters from the beginning of the data.