How to Keep Track of row index when JTable has been sorted by the user?

前端 未结 3 1095
借酒劲吻你
借酒劲吻你 2021-01-11 15:38

I have a JTable which has 1st row blank. Now when I sort the table based on a column by clicking on that column then the blank row goes at the bottom. If I insert something

3条回答
  •  终归单人心
    2021-01-11 15:57

    SOLUTION 1

    There is some issue when using jTable1.convertRowIndexToModel(jTable1.getSelectedRow()) if you sort the values according to date or time will return incorrect values from the convertRowIndexToView(0) therefore use convertRowIndexToModel(0).

    SOLUTION 2

    This will retrieve from the new model when sorted.

    jTable1.getValueAt(jTable1.getSelectedRow(), 0);
    

    This will retrieve value according to the original index even when sorted.

    ((DefaultTableModel)jTable1.getModel()).getValuAt(jTable1.getSelectedRow(), 0);
    

提交回复
热议问题