简体   繁体   English

如何在jtextfield中以输入字符开头过滤jtable行

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

I have created JTable row filter, which is working well and filtering rows according to typing in JTextfield, but it filters according to typed character present anywhere in the row whereas I want to filter the row starting with the typed character . 我创建了JTable行过滤器,该过滤器运行良好,并且可以根据JTextfield中的键入来过滤行,但是它会根据行中任意位置出现的键入字符进行过滤,而我想从键入字符开始过滤行 Is there any regex flag for it? 是否有任何正则表达式标志?

My table row filter code: 我的表格行过滤器代码:

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 但它会根据行中任何位置出现的键入字符进行过滤

It filters based on the data found in the column specified by columnIndex. 它根据在columnIndex指定的列中找到的数据进行过滤。

I want to filter the row starting with the typed character. 我想过滤以键入字符开头的行。

If you are saying you want to filter based on matching from the first character of the data found in the specified column then you should be able to use: 如果您说要基于从指定列中找到的数据的第一个字符开始的匹配进行过滤,那么您应该可以使用:

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

Read the API for the Pattern class. 阅读Pattern类的API。 The Boundary Matchers section shows that the "^" is used to match characters from the beginning of the data. Boundary Matchers部分显示“ ^”用于匹配数据开头的字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM