从 jtable 检索布尔值
在我的 Jtable 中,有一列布尔值显示为复选框。 仅当值为 true 时我才能检索值单元格,当值为 false 时我无法读取值。 我编写代码:
int row = jTMezziInt.getRowCount();
int h=0;
while (h<=row){
chk= ((Boolean)jTMezziInt.getValueAt(h, 6)).booleanValue();
//if chk is true I can read;
// if chk is false the execution stopped at the chk assignement;
if (chk)
((DefaultTableModel )this.jTMezziInt.getModel()).removeRow(h);
row = jTMezziInt.getRowCount();
h=h+1;
}
TableColumn Selez = jTMezziInt.getColumnModel().getColumn(6);
}
为了定义表,我使用带有表编辑器的 netbeas。
感谢大家的帮助;
in my Jtable I have a column with boolean values displayed as checkbox.
I can retrive the value cell only when the value is true, when values i false I can't read the values.
I write my code:
int row = jTMezziInt.getRowCount();
int h=0;
while (h<=row){
chk= ((Boolean)jTMezziInt.getValueAt(h, 6)).booleanValue();
//if chk is true I can read;
// if chk is false the execution stopped at the chk assignement;
if (chk)
((DefaultTableModel )this.jTMezziInt.getModel()).removeRow(h);
row = jTMezziInt.getRowCount();
h=h+1;
}
TableColumn Selez = jTMezziInt.getColumnModel().getColumn(6);
}
For define the table I had used netbeas with table editor.
Thanks everybody for help;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,这里有两个可能的问题之一。最可能的问题是您从 h = 0 开始,然后以 h = row 结束(因为 while 循环在 h <= row 时进行迭代)。
举例来说,您的表中有 3 行。该循环现在将针对 h = 0、h = 1、h = 2 和 h = 3 运行,即运行 4 次,但只有 3 行(索引为 0、1 和 2,没有索引为 3 的行)。这将在其最终迭代中导致空指针异常。这是您看到的行为吗?
要解决这个问题,只需使 while 循环条件 h <行,而不是 h <= 行。如果这不起作用,请告诉我,我们可以讨论其他可能的问题。
另一个问题是,即使您正在检查的行已被删除,您仍然会增加 h 。如果我们检查第 1 行,然后删除第 1 行,那么第 2 行将成为第 1 行,因此我们需要重新检查新的第 1 行。因此,如果您正在检查的当前行未被删除,则只应增加 h 。
最后,顺便说一句,请注意,如果您使用的是较新的 JDK 之一,则无需调用 booleanValue() (这称为不必要的拆箱)。这将自动为您完成。因此,您可以将以下内容更改
为:
它只是更整洁、更好的样式。
There are one of two possible issues here from what I can see. The most likely issue is that you start with h = 0 and then end with h = row (since your while loop iterates while h <= row).
Say for example you have 3 rows in your table. This loop will now run for h = 0, h = 1, h = 2 and h = 3, i.e. it runs 4 times but you only have 3 rows (indexed 0, 1 and 2 there is no row with index 3). This would cause a null pointer exception on its final iteration. Is this the behaviour you are seeing?
To sort it out just make your while loop condition h < row, not h <= row. If this does not work then let me know and we can discuss the other possible issue.
Another issue is that you still increment h even if the row you are checking is deleted. If we are checking row 1 and then delete row 1 then row 2 will become row 1, so we need to recheck this new row 1. So you should only increment h if the current row you are checking is not deleted.
Finally, as an aside, note that if you are using one of the later JDKs there is no need to call booleanValue() (this is called unnecessary unboxing). This will be done automtically for you. So you can change the following:
to:
It's just neater and better style.