0

我有一个 Jtable,我想根据用户条目对其进行过滤。我写了这段代码:

data_table = new JTable(model); // create data table

data_table.setModel(new DefaultTableModel(pin, colNames)); // fill jtable
model = new DefaultTableModel(pin, colNames); // create a table model

final TableRowSorter<TableModel> sorter;

sorter = new TableRowSorter<TableModel>(model);

data_table.setRowSorter(sorter);

点击我有这个代码:

String text = filter_txtf.getText(); // i get the user entry from a text field
    if (text.length() == 0) {
     sorter.setRowFilter(null);
      //data_table.setRowSorter(sorter);  
    } else {
      try {
        sorter.setRowFilter(
            RowFilter.regexFilter(text));
        //data_table.setRowSorter(sorter);  
      } catch (PatternSyntaxException pse) {
        System.err.println("Bad regex pattern");
     }
   }

问题是单击时什么也没有发生,我调试我的程序并且正常调用了“actionperformed”。我的代码有什么问题?

4

1 回答 1

2

Basic code looks reasonable, but we can't see the context of how the code is used since you didn't provide a SSCCE. No need for if/else statement, just always reset the row filter.

See the section from the Swing tutorial on Sorting and Filtering for a working example.

于 2013-05-20T15:02:52.960 回答