JTable如何保存新的Row索引
我有以下问题。 假设我有一个具有以下值的 JTable:
1 2 3 4 5
现在我选择“3”并按下一个按钮来保存行的索引并打印“3” 我有另一个按钮,我们称之为“下一个按钮”,当我按下它时,它会打印“4”
现在我对整个表进行排序(或者在这种情况下只是随机化),现在是这样的:
2 5 4 1 3
当我现在按“下一步按钮”时,我希望它打印“1”,因为它是“4”之后的值。
我怎样才能让它发挥作用? 当我不对表进行排序时,它已经可以工作了,我可以只打印保存的行+1...
是否有类似排序侦听器的东西,以便我可以用新值覆盖保存的行?
干杯蒂莫西
I have the following Problem.
Let's Say I have a JTable with the folowing Values:
1
2
3
4
5
Now I select "3" and press a button which saves the index of the row and prints "3"
I have another Button let's call it the "next button", when I press it it will print "4"
Now I sort (Or in this Case just randomise) the whole Table and it's now like this:
2
5
4
1
3
When I now press the "next button" I want it to print "1" because it's the value after "4".
How can I manage this to work ?
It already works when I'm not sorting the Table than I can just print the saved row +1...
Is there something like a sort Listener so I can overwrite the saved row with the new value?
Cheers Timothy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您深入了解
TableModel
的世界。在 Swing 中编程时,通常最好立即将模型从组件中分离出来。向表中添加一列来处理订单,但不显示它。一个更简单但有点死胡同的方法是让对象存储在您自己设计的类型的表中。只需重写 toString 即可返回显示表示形式。
I suggest you dive into the world of
TableModel
. When programming in Swing it is usually best to split the model from the component straight away. Add a column to the table to handle order, but don't show it.A simpler method, but a bit of a dead end, is to make the the object stored in the table of a type of your own devising. Just override
toString
to return the display representation.据我了解,问题出在最后一个索引上,在你的问题中,当它是 4 时,下一个索引应该是 1 。这个问题可以通过设置 if 条件来完成。
例如,如果我假设您将索引保存在变量 rowIndex = 4; 中
if(rowIndex ==jTable.getRowCount()-1)
下一个行性别 = 1 ; // 这是第一个索引
As i understand that the problem is with the last index , in ur problem that when it's 4 the next one should be 1 . this problem can be done by makeing if condition .
fore example if i suppose u save the index in a varible rowIndex = 4;
if(rowIndex ==jTable.getRowCount()-1)
nextRowinsex = 1 ; // which is the first index