【问题标题】:How to filter jtable row starting with the typed character in jtextfield如何过滤以 jtextfield 中键入的字符开头的 jtable 行
【发布时间】:2018-05-22 19:11:41
【问题描述】:

我创建了 JTable 行过滤器,它运行良好并根据 JTextfield 中的输入过滤行,但它根据行中任何位置存在的输入字符进行过滤而我想过滤以输入字符开头的行。 是否有任何正则表达式标志?

我的表格行过滤代码:

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);
}

【问题讨论】:

    标签: java regex swing jtable rowfilter


    【解决方案1】:
    rf = RowFilter.regexFilter("(?i)" + value, columnIndex);  
    

    但它会根据行中任何位置出现的键入字符进行过滤

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

    我想过滤以输入字符开头的行。

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-20
      • 1970-01-01
      • 2013-10-22
      • 1970-01-01
      • 2018-10-21
      • 1970-01-01
      • 2020-09-04
      相关资源
      最近更新 更多