如何过滤以jtextfield中键入的字符开头的jtable行

How to filter jtable row starting with the typed character in jtextfield

我已经创建了 JTable 行过滤器,它运行良好并根据在 JTextfield 中键入来过滤行,但它根据行中任何位置出现的键入字符进行过滤 而我想过滤从开始的行使用键入的字符。 是否有任何正则表达式标志?

我的 table 行过滤器代码:

public static void setFilter(JTable table,String value) {
    sorter = new TableRowSorter<>((DefaultTableModel)table.getModel());
    table.setRowSorter(sorter);
    RowFilter<DefaultTableModel, Object> rf = null;
    try {
        rf = RowFilter.regexFilter("(?i)" + value, columnIndex);   //("(?i)" for case insensitive filter
    } catch (java.util.regex.PatternSyntaxException e) {
        return;
    }
    sorter.setRowFilter(rf);
}
rf = RowFilter.regexFilter("(?i)" + value, columnIndex);  

but it filters according to typed character present anywhere in the row

它根据在 columnIndex 指定的列中找到的数据进行过滤。

I want to filter the row starting with the typed character.

如果您说要根据在指定列中找到的数据的第一个字符的匹配进行过滤,那么您应该能够使用:

rf = RowFilter.regexFilter("^" + value, columnIndex);  

阅读 API Pattern class。 Boundary Matchers 部分显示“^”用于匹配数据开头的字符。