在删除连续行后动态更新JTable行 [英] Updating JTable row dynamically after the deletion successive rows

查看:391
本文介绍了在删除连续行后动态更新JTable行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JTable,我动态插入行到该JTable。
在一些用户干预后,将插入一个新行。然后我将调用一个函数的参数作为选定的行号,并在该函数内,我有一些代码,将更新相同的行相应。每个行插入和行值更新代码将在单独的线程中运行。

  public void updateRow(int row,JTable myTable)
{
String text =;
//经过很多处理,在第4列的'row'设置表格单元格的值
myTable.setValueAt(text,row,4);
}

我面临的问题如下,



如果用户删除任何行,那么行的位置将改变,那时如果函数 updateRow()试图更新一些其他行,那么它会因为行数更改而失败。



假设我每次有3行,每行的updateRow正在进行。



updateRow(0,userTable); //对于第一行
updateRow(1,userTable); //对于第二行
updateRow(2,userTable); //对于第三行

完成了。这将导致第3行的updateRow()函数的问题。因为,它的行值为2。由于第三行被删除,因此没有第三行,从而导致以下代码失败。

  myTable.setValueAt text,row,4); //目前,row的值为'2'

建议我如何能够用行值相应地跟踪行更新,即使行的位置动态改变了吗?



提前感谢。

解决方案

而不是依赖于视图的行号,直接操作模型,如在@mKorbel的注释中建议的;如果你改变模型,视图将遵循。如此处所述, JTable 提供了将模型坐标转换为视图坐标的方法 - convertColumnIndexToView convertRowIndexToView 从视图坐标转换为模型坐标 - convertColumnIndexToModel convertRowIndexToModel


I have a JTable and I am inserting rows to that JTable dynamically. After some user intervention, a new row will be inserted .Then I will call a function with parameter as selected row number and inside that function, I have some code which will update the same row accordingly. Each row insertion and row value update code will run in a separate thread.

public void updateRow(int row,JTable myTable)
{
  String text = "";
 //After lot of processing, setting the table cell value at the 'row' on the 4th column
  myTable.setValueAt(text, row, 4);
}

The issue I am facing is as follows,

If the user deleting any row, then row's position will change and at that time if the function updateRow() trying to update some other row, then it will fail because of change of row count.

Lets say I have 3 row at a time and each row's updateRow is in progress.

updateRow(0,userTable);//For the 1st row
updateRow(1,userTable);//For the 2nd row
updateRow(2,userTable);//For the 3rd row

and assume 2nd row's updateRow() is completed. This will cause issue in updateRow() function of 3rd row. Because, it has the row value as '2' . Since the 3rd row got deleted, there is no 3rd row and which in turn cause the following code to fail

myTable.setValueAt(text, row, 4);//Currently, row has the value as '2'

Can anyone suggest me how can I keep track of the row update with row values accordingly, even if the row's position got changed dynamically ?

Thanks in advance.

解决方案

Instead of relying on the view's row number, operate on the model directly, as suggested in a comment by @mKorbel; if you alter the model, the view will follow. As discussed here, "JTable provides methods that convert from model coordinates to view coordinates — convertColumnIndexToView and convertRowIndexToView — and that convert from view coordinates to model coordinates — convertColumnIndexToModel and convertRowIndexToModel.

这篇关于在删除连续行后动态更新JTable行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆