JTable removeRow(), removing wrong row

后端 未结 2 643
别跟我提以往
别跟我提以往 2020-12-12 04:56

I have a JTable and I need to remove a row, namely, the selected row.

So first, I get the the table model :

    DefaultTableModel model = (DefaultTab         


        
相关标签:
2条回答
  • 2020-12-12 05:31

    The index returned by JTable.getSelectedRow is a view index : it's the index of the row as seen by the end-user in the table. It's not the same as the model index, because if you sort the table, the indices in the model don't change, but the indices in the view do change. So, you must always use JTable.convertRowIndexToModel to get the model index from the view index.

    Note that the same must be done for columns, because the user might choose to reorder columns to its taste.

    Also, you shouldn't have to set the model again each time you remove a row. Instead, your model should fire a TableModelEvent to inform the view about the removal. See AbstractTableModel.fireTableRowsDeleted.

    0 讨论(0)
  • 2020-12-12 05:33

    If is JTable filtered or sorted then you can convert

    int modelRow = convertRowIndexToModel(row);
    
    0 讨论(0)
提交回复
热议问题