JTable值“跳跃” 按 Enter 键后在另一个单元格中

发布于 2024-07-12 21:43:28 字数 299 浏览 4 评论 0原文

我正在使用 Java。 我创建了一个 JTable,并且使表的单元格可编辑。 我的问题是,当我选择一个单元格并在其中写入一个值,然后单击 Enter 时,我键入的值不会保留在我键入它的单元格中,而是跳转到表中某处的另一个单元格中。 该值保持不变,但在其上是我输入的单元格。

我将表的单元格表示为 Cell 类的实例。

默认情况下会发生一些事情,尽管我在网上查阅了一些有关 JTables 的资料,但我不知道要更改什么才能使单元格的值保留在单元格中。 您知道问题可能是什么,以及我应该在代码中查找何处才能修复它吗? 先感谢您。

I am working with Java. I have created a JTable, and I have made the cells of the table editable. My problem is that when I select a cell and write a value in it, and then click Enter, the value, which I have typed does not stay in the cell where I have typed it, but jumps in another cell somewhere in the table. The value stays the same, but on it the cell, which I have typed it.

I represent the cells of the table as instances of class Cell.

Something happens by default, and I don't know what to change in order to make the value of the cell stays in the cell, though I looked through some materials online about JTables. Do you know what the problem might be, and where should I look in the code in order to fix it? Thank you in advance.

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

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

发布评论

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

评论(1

浮云落日 2024-07-19 21:43:28

看起来你的模型索引在某个地方被混淆了。

如果您完全使用 RowSorter,那么 JTable.getSelectedRow 将返回正在查看的行的索引,而不是模型中的行。 您可以使用 RowSorter.convertRowIndexToModel() 来获取正确的索引。

仔细检查您的 JTableModel.setValueAt() 方法,以确保单元格索引是您已编辑的索引,并且您将新值存储在正确的位置。

It looks like your model indexes are being confused somewhere.

If you are using a RowSorter at all, then JTable.getSelectedRow will return the index of the row being viewed, not the row in the model. You can use RowSorter.convertRowIndexToModel() to get the correct index.

Double check your JTableModel.setValueAt() method to make sure the cell indexes are the ones you have edited, and that you're storing the new value in the right place.

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