基于两个不同列的值的JTable行过滤

时间:2013-07-03 14:24:10

标签: java swing jtable nested rowfilter

我想基于两个不同列的值在JTable中实现行过滤:

column1 = 1

column2 = 5

以下是基于INDEX_FIELD = 1条件执行行过滤的方法:

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

rowFiltering(1);

如何基于两个值实现行过滤?有点像...

rowFiltering(valueColumn1, valueColumn2);

2 个答案:

答案 0 :(得分:8)

使用and过滤器:

//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);

上述代码是根据Sorting and Filtering中的示例修改的。

答案 1 :(得分:2)

根据@camickr解决方案,这是我自己问题的代码。

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);
}

所以我可以按如下方式调用该方法:

filterRowsResults(valueC1, valueC2);