javaswingjtablerowsorter

Getting true selected value of JTable


I'm currently getting the value of my jTable as everybody does:

String  d = jTable1.getModel().getValueAt( jTable1.getSelectedRow() , row ).toString();

The thing is that now I'm sorting my jTable with a rowsorter:

sorter = new TableRowSorter<TableModel>(modelo);
jTable1.setRowSorter(sorter);

private void filterTable(){
        //If current expression doesn't parse, don't update.
        try {
            rf = RowFilter.regexFilter("(?iu)"+jFilter.getText(),0,1,2,3,4);//no filtrar la columna con imágenes porque hace cualquiera
        } catch (java.util.regex.PatternSyntaxException e) {
            return;
        }
        sorter.setRowFilter(rf);
    }

THE PROBLEM: Once the table is filtered, the function getSelectedRow returns the correct row, but the getModel function returns the original model, not the one after filtering...

THE QUESTION: How to get the correct value from the table when it is filtered?


Solution

  • For future wanderers:

    The problem was on the way of getting the value from the jTable:

    This is wrong when you have a rowsorter:

    String  d = jTable1.getModel().getValueAt( jTable1.getSelectedRow() , row ).toString();
    

    This is what you should be doing:

    String  d = jTable1.getValueAt( jTable1.getSelectedRow() , row ).toString();