javaswingsortingjtablerowsorter

Update model after make setAutoCreateRowSorter is true


 tableMalzeme.setModel(DbUtils.resultSetToTableModel(resultSet));               
 tableMalzeme.setAutoCreateRowSorter(true);

It is sorting but model does not change. I called model of jtable. but it comes before sorting.

txtmalzeme.setText(tableMalzeme.getModel().getValueAt(tableMalzeme.getSelectedRow(), 1).toString());

How to update model after make setAutoCreateRowSorter is true ?


Solution

  • If you want to access the value from the selected row in the table then you need to use:

    //txtmalzeme.setText(tableMalzeme.getModel().getValueAt(tableMalzeme.getSelectedRow(), 1).toString());
    txtmalzeme.setText(tableMalzeme.getValueAt(tableMalzeme.getSelectedRow(), 1).toString());
    

    That is you need to access the data via the table, since the table knows the current display order of the data. The data in the TableModel is never actually sorted, so you can't reference it by the selected row in the table.

    If you want to access the data via the TableModel then you first need to convert the selectedRow value to the actual model row by using:

    table.convertRowIndexToModel(...)