简体   繁体   English

如何检查JTable JAVA中是否存在行?

[英]How can I check if a row exists in JTable JAVA?

I searched but there are no similar questions. 我搜索了,但是没有类似的问题。

I have a function with 2 parameters: row_num, value Before adding a new value, I need to check to see if that row exists in the JTable or not. 我有一个带有2个参数的函数:row_num,value在添加新值之前,我需要检查该行是否在JTable中存在。 If it is not, I will add a null row before adding value. 如果不是,我将在添加值之前添加一个空行。

The problem is I don't know how to check if it exists. 问题是我不知道如何检查它是否存在。 I tried table.isCellEditable(row_num, 2) but I'm not sure why it always returns True though the table doesn't contain that row. 我尝试了table.isCellEditable(row_num, 2)但是我不确定为什么表不包含该行却总是返回True

There's not much to go on here. 这里没有什么可做的。 Just check the table row count: 只需检查表的行数:

DefaultTableModel dtm = (DefaultTableModel) yourTable.getModel();
int rows = dtm.getRowCount();

If the value in row_num is greater than rows then the row obviously doesn't exist yet. 如果row_num中的值大于行,则该行显然尚不存在。 If row_num is working off of the JTable index value which is 0 based then you would need to subtract 1 from rows or: 如果row_num是基于基于0的JTable索引值计算的,则需要从行中减去1或:

if (row_num < rows) { 
    System.out.println("Yup...The row number " + row_num + " does exist!");
    // Call your specific method here....
}

Or is it that you are trying to compare a value contained within a row specific row cell in which case we would also need the column number of that specific cell. 还是正在尝试比较特定于行的行单元格中包含的值,在这种情况下,我们还需要该特定单元格的列号。 Maybe I'm just not grasping your question properly and I have this all wrong in which case I'll simply delete this answer. 也许我只是无法正确理解您的问题,而我全都错了,在这种情况下,我将直接删除此答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM