Preserve JTable selection across TableModel change

前端 未结 7 1768
北荒
北荒 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:26

    I had the same issue in an application. In my case the model in the table was a list of objects, where the object properties where mapped to columns. In that case, when the list was modified, I retrieved the selected index and stored the object that was selected before updating the list. After the list is modified and before the table is updated, I would calculate the position of the selected object. If it was still present after the modification, then I would set the selection to the new index.

    Just setting the selected index in the table after the modification will not work, because the object may change position in the list.

    As a side note, I found that working with GlazedLists makes life much easier when dealing with tables.

提交回复
热议问题