javaswingjtabletablemodelrowsorter

update jtable model after filtering


I want to apply the filter to the JTable.

            String text = textField.getText();
            rowSorter = new TableRowSorter<>(tableModel);
            this.getjTable1().setRowSorter(rowSorter);
            this.getjTable1().removeAll();
            if (text.trim().length() == 0) {
                rowSorter.setRowFilter(null);
            } else {
                //String regex = String.format("^%s$", text);
                if(jCheckBoxExtract.isSelected()){
                    text="^"+text+"$";
                }
                else{
                    if(!text.contains(".")||text.contains("$"))text="^"+text;
                }
                RowFilter rowFilter = RowFilter.regexFilter(text, 1);
                rowSorter.setRowFilter(rowFilter);
            }
            this.getjTable1().repaint();   

this code work but now, if I want to get a value in jtable, the model doesn't update. The model use in jtable is always the old model but not the new model after filter.


Solution

  • to get the proper value do this whenever you need the row from the table:

    model.getSelectedEntry(table.convertRowIndexToModel(table.getSelectedRow()));