How can I filter rows in a JTable?

后端 未结 4 1779
旧时难觅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 08:49

    You can use JTable.setAutoCreateRowSorter which will use the default row sorter/filter of the JTable

    0 讨论(0)
  • 2020-12-06 08:49

    To pick up the comment from kd304, you could use GlazedLists. There you'll use a FilterList as the input for your JTable, and the FilterList will take care of the rest.

    0 讨论(0)
  • 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<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);
    }
    
    0 讨论(0)
  • 2020-12-06 09:04

    This few line solution seems to work:

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) 
    {                                            
        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(((DefaultTableModel) jTable1.getModel())); 
        sorter.setRowFilter(RowFilter.regexFilter(jTextField1.getText()));
    
        jTable1.setRowSorter(sorter);
    }  
    
    0 讨论(0)
提交回复
热议问题