且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

保持对TableModel更改的JTable选择

更新时间:2023-12-04 13:05:52

您需要保留选择然后重新应用它。

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.

然后当你重新开始使用您需要以编程方式重新应用相同选择的新数据加载JTable。

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.

在模型更新之前,用户可以选择第2行第1列,其值为Duck。但是在模型更新之后,相同的数据现在可以在第4行第1列中出现,并且您的原始单元格第2列第1列可能具有新数据,例如Pig。现在,如果您强制将选择设置为模型更新之前的选择,则可能不是用户想要的。

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.