Preserve JTable selection across TableModel change

前端 未结 7 1616
北荒
北荒 2021-02-07 11:52

We\'re seeing JTable selection get cleared when we do a fireTableDataChanged() or fireTableRowsUpdated() from the TableModel.

7条回答
  •  终归单人心
    2021-02-07 12:34

    You need to preserve the selection and then re-apply it.

    First of all you will need to get a list of all the selected cells.

    Then when you re-load the JTable with the new data you need to programmatically re-apply those same selections.

    The other point I want to make is, if the number or rows or columns in your table are increasing or decreasing after each table model reload, then please don't bother preserving the selection.

    The user could have selected row 2 column 1 having a value say "Duck", before model updation. But after model updation that same data can now occur in row 4 column 1, and your original cell row 2 column 1 could have new data such as "Pig". Now if you forcibly set the selection to what it was before the model updation, this may not be what the user wanted.

    So programmatically selecting cells could be a double edged sword. Don't do it, if you are not sure.

提交回复
热议问题