当列重新排序时,填充数据库数据的 JTable 不会更新

发布于 2024-08-28 22:36:39 字数 522 浏览 3 评论 0原文

我有一个 JTable,其中填充了数据库表的数据(因此我使用了 ResultSetTableModel),并在单击 JTable 的一列时使用 TableRowSorter 对行进行排序。数据在jTable中显示没有问题;

但是,当我按某些列表对 JTable 进行排序(例如,按主键值对其进行排序),并编辑排序后的 jTable 中的某些单元格时,更改的值是列排序之前位于该位置的旧单元格。

例如: 假设我有一个包含 2 列的表格 - 姓名年龄。我的表有以下数据:

c 1
b 2
a 3

当我在JTable中按名称对i进行排序时,

a 3
b 2
c 1

如果我编辑值“1”,它会变成这样,编辑后,表会变成这样

a 1
b 2
c 1

看起来位置没有在JTable中更新JTable,并根据其原始位置对值进行编辑。

I have a JTable filled with data of a table of my database (so I used ResultSetTableModel) and using TableRowSorter to sort the rows, as I click in one column of the JTable. The data is displayed in the jTable without problems;

But when I sort the JTable by some column table (for example, sorting it by the primary key value), and edit some cell from the sorted jTable, the value changed is the old cell that were in that position before the ordenation of the column.

For example:
Suppose I have a table with 2 columns - name and age. My table has the following data:

c 1
b 2
a 3

when I order i by name in the JTable, it becomes like this

a 3
b 2
c 1

if I edit the value "1", after the edition, the table becomes like this

a 1
b 2
c 1

It seems that the positions are not being updated in the JTable, and the values are edited considering their original positions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风柔一江水 2024-09-04 22:36:39

首先,请注意数据实际上包含在模型中,而 jtable 只是一个视图。
通常,默认情况下,jtable 的行和列与表的行和列相对应。当你排序时,jtable的row,col之间的映射可能不会保持不变,
所以当你想编辑单元格(rowVal,colVal)时,
这样做

table.setValueAt(object,table.convertRowIndexToModel(rowVal),convertColumnIndexToModel(colVal))

应该在排序后保留映射。
请阅读此处的“排序和过滤”部分:http://java. sun.com/docs/books/tutorial/uiswing/components/table.html

Firstly, note that data is actually contained in the model and jtable is just a view.
Normally, by default the jtable's rows and columns correspond to rows and columns of the table. When you sort, this mapping between jtable's row,col may not remain the same,
so when you want to edit say cell(rowVal,colVal),
do

table.setValueAt(object,table.convertRowIndexToModel(rowVal),convertColumnIndexToModel(colVal))

this should preserve the mapping after sorting.
Read the section Sorting and Filtering here: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文