多列上的Java RowFilter

时间:2016-04-13 14:09:18

标签: java filter jtable rowfilter tablefilter

我想在多列上对JTable进行排序/过滤,f .E。

Column1  Column2
   a        1
   b        2
   c        2

我想通过 Column1 = b Column2 = 2 对此表进行排序。

MyTableModel model = new MyTableModel();
sorter = new TableRowSorter<MyTableModel>(model);
table = new JTable(model);
table.setRowSorter(sorter);

...

private void newFilter() {
    RowFilter<MyTableModel, Object> rf = null;
    //If current expression doesn't parse, don't update.
    try {
        rf = RowFilter.regexFilter(filterText.getText(), 0);
    } catch (java.util.regex.PatternSyntaxException e) {
        return;
    }
    sorter.setRowFilter(rf);
}

但是如何对多个列进行排序?

2 个答案:

答案 0 :(得分:2)

您可以创建RowFilter.AndFilter并传递所需的所有过滤器:

List<RowFilter<M, O>> listOfFilters = new ArrayList<>();
... add filters into list
RowFilter.andFilter(listOfFilters);

答案 1 :(得分:0)

只需删除参数&#34; 0&#34;从RowFilter对所有列进行排序:

rf = RowFilter.regexFilter(filterText.getText());