JTable row filtering based on the values โ€‹โ€‹of two different columns

I want to implement row filtering in JTable based on the values โ€‹โ€‹of two different columns:

column1 = 1

column2 = 5

Here is a method that filters rows based on the 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 string filtering based on two values? Sort of...

 rowFiltering(valueColumn1, valueColumn2); 
+4
source share
2 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 has been modified from an example found in Sorting and Filtering .

+8
source

Here is the code for my own question, according to @camickr's solution.

 public void filterRowsResults(int x1, int x2) { List<RowFilter<ResultsModel, Integer>> filters = new ArrayList<RowFilter<ResultsModel, Integer>>(2); RowFilter<ResultsModel, Integer> filterC1 = RowFilter.numberFilter(ComparisonType.EQUAL, x1, 1); RowFilter<ResultsModel, Integer> filterC2 = RowFilter.numberFilter(ComparisonType.EQUAL, x2, 2); filters.add(filterC1); filters.add(filterC2); RowFilter<ResultsModel, Integer> filter = RowFilter.andFilter(filters); resultsTableSorter.setRowFilter(filter); } 

Therefore, I can call the method as follows:

 filterRowsResults(valueC1, valueC2); 
+2
source

Source: https://habr.com/ru/post/1489512/


All Articles