JTable RowFilter - case insensitive

128 views Asked by At

I'm having a problem with a RowFilter, I'm trying to make it display data without it being case sensitive.

DefaultTableModel table=(DefaultTableModel)tablicaEv.getModel();
String search=jTextField1.getText();
TableRowSorter<DefaultTableModel> tr=new TableRowSorter<> (table);
tablicaEv.setRowSorter(tr);
tr.setRowFilter(RowFilter.regexFilter(search);

I tried adding "(?!)" in the RegexFilter but that only makes my table not display any data at all when I try searching. What am I doing wrong?

2

There are 2 answers

0
Thomas Kläger On BEST ANSWER

You probably wanted to prefix the search string with (?i) (case insensitive match).

(?!) is a "zero-width negative lookahead" matching nothing which will never match if anything follows it.

0
lawever xu On

try this

    public static RowFilter<Object, Object> getCaseInsensitiveRowFilter(String text, int... indexes) {
        if (com.csii.cide.base.util.StringUtil.isBlank(text)) {
            return null;
        }
        List<RowFilter<Object, Object>> filters = Lists.newArrayList();
        filters.add(RowFilter.regexFilter(text.toUpperCase(), indexes));
        filters.add(RowFilter.regexFilter(text.toLowerCase(), indexes));
        return RowFilter.orFilter(filters);
    }