如何过滤JTable中的行? [英] How can I filter rows in a JTable?

查看:235
本文介绍了如何过滤JTable中的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JTable,其中包含很多字符串。我在表格上方创建了一个用于用户输入的文本框。我想要一个行过滤器,可以删除用户在文本框中输入的行。 。请帮我出这个

i have a JTable having many strings in that.i have created a textbox for user entry, above the table. i want a row filter which can remove the rows having strings enterd by the user in the text box. please help me out for this.

推荐答案

从这里开始:点击
排序和过滤

在下面的示例代码中,
显式创建一个分类器对象,所以
稍后您可以使用它来指定
过滤器:

In the following example code, you explicitly create a sorter object so you can later use it to specify a filter:

MyTableModel model = new MyTableModel();
sorter = new TableRowSorter<MyTableModel>(model);
table = new JTable(model);
table.setRowSorter(sorter);

然后根据文本字段的当前
值进行过滤:

Then you filter based on the current value of a text field:

private void newFilter() {
    RowFilter<MyTableModel, 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);
}


这篇关于如何过滤JTable中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆