Pergunta

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);
Foi útil?

Solução

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.

Outras dicas

Here is the code for my own question, according to @camickr 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);
}

So I can call the method as follows:

filterRowsResults(valueC1, valueC2);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top