I have a JTable with four columns, the first one containing either a number or a text, the other three only text. I’m trying to filter this table with the help of a RowFilter:
sorter = new TableRowSorter<TableModel>(myOwnTableModel);
The checkboxFilter I got works well enough:
sorter.setRowFilter(RowFilter.regexFilter("^[0-9]$", 0));
This sorter is activated or deactivate depending on a checkbox that is either set or not.
The second filtering happens if a user puts some text in a textfield. For itself, this works fine already:
String regex = "(?i)" + Pattern.quote(s); // s = input Text of user
sorter.setRowFilter(RowFilter.regexFilter(regex, 1,2,3));
What I can’t do, is to activate both filters at the same time. Maybe I’m thinking way too far, my idea has been to “concatenate” the two filters, the checkboxFilter should be “and” the other “or”. I tried several things, to me the most promising looked something like:
String regex = "(?i)" + Pattern.quote(s);
bookFilter = RowFilter.regexFilter(regex, 1,2,3);
sorter.setRowFilter(bookFilter.andFilter(Arrays.asList(
RowFilter.regexFilter("^[0-9]$", 0))));
Unfortunately, this doesn’t lead to any usable result. Any ideas appreciated 🙂
The solution is to add an
ActionListenerto theJCheckBoxto update the filter state if the checkbox is toggled and to add aDocumentListenerto theJTextField‘s underlying Document to update the filter state if the contents of the field is updated.The other bug in your code is that you are calling the static
andFiltermethod on yourbookFilterinstance and are only passing in the newly constructed regex filter (i.e. you are only passing in one parameter toandFilter). The correct usage is:Example Event Listeners
… and then define your updateFilters() method to install a new filter based on when the checkbox is selected and whether the text field is empty or not.
Example Filter Update Method