How can I filter rows in a JTable?

后端 未结 4 1788
旧时难觅i
旧时难觅i 2020-12-06 08:13

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

4条回答
  •  感情败类
    2020-12-06 09:04

    from here:
    sorting and filtering

    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(model);
    table = new JTable(model);
    table.setRowSorter(sorter);
    

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

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

提交回复
热议问题