Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JTable row filtering based on values of two different columns

I want to implement row filtering in a JTable, based on values of two different columns:

column1 = 1

column2 = 5

Here is the method that performs row-filtering based on INDEX_FIELD = 1 condition:

    public void rowFiltering(int x) {
    RowFilter<ResultsModel, Integer> IDfilter = RowFilter.numberFilter(
            ComparisonType.EQUAL, x, column1);
    resultsTableSorter.setRowFilter(IDfilter);
}

rowFiltering(1);

How can I implement row filtering based on two values? Something like...

rowFiltering(valueColumn1, valueColumn2);
like image 882
capovawi Avatar asked Dec 21 '22 03:12

capovawi


1 Answers

Use the and filter:

//rf = RowFilter.regexFilter(filterText.getText(), 0);
List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(2);
filters.add(RowFilter.regexFilter(filterText.getText(), 0));
filters.add(RowFilter.regexFilter(filterText.getText(), 1));
rf = RowFilter.andFilter(filters);

The above code was modified from the example found in Sorting and Filtering.

like image 152
camickr Avatar answered Feb 20 '23 19:02

camickr