Search code examples
javaswingjtablerowfilter

Filtering in Java Swing Table


public void newFilter() {
    RowFilter<ListTable, 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);

}

I'm using this filter from http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting But this filter seems only able to filter int component on my table (Apart from the first col, all others are strings) . I simply want to create a Filter that leave any row with entered text. I'm relatively new to the language, Thanks very much for your help!

https://i.sstatic.net/WxOmz.jpg Here is 3 Image


Solution

  • Your filter is explicitly applied to column zero, the first column. As shown here, you can include all columns by omitting the indices parameter:

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

    Alternatively, you can include specified columns, e.g. columns one through three:

    rf = RowFilter.regexFilter(filterText.getText(), 1, 2, 3);
    

    A related example that combines filters is seen here.

    Addendum: In helpful comments, @Ordous reminds me that correct usage hinges on the varargs feature, new in Java 5.