gpt4 book ai didi

java - jTable 仅选择选中复选框的数据

转载 作者:行者123 更新时间:2023-12-01 22:42:52 24 4
gpt4 key购买 nike

您好,我在 jTable 中的单独数据方面遇到问题。

我的jTable:

enter image description here

当我单击第二个按钮时,jTable 中有两个数据。 enter image description here

这是正确的,但是当我再次单击第一个按钮时,我得到与第二个按钮中的 jTable 相同的 jTable。

enter image description here

这是我的代码:

DefaultTableModel model = new DefaultTableModel();
model = (DefaultTableModel) jTable1.getModel();

for(int i = 0; i < jTable1.getRowCount(); i++)
{
if(jTable1.getValueAt(i, 0) == Boolean.FALSE)
{
model.removeRow(i);
}
}

jTable2.setModel(model);

但是这不起作用。有谁告诉我为什么吗?

最佳答案

But it doesn't work. Is there anyone who tells me why ?

例如,当您删除第 0 行时,所有行都会下移 1,但“I”的值会不断增加 1,因此每次删除行时都会“跳过”行。

解决方案是从最后一行开始并递减“I”:

for(int i = jTable.getRowCount() - 1; i >=0; I--)

It's not a problem.

是的。您发布的示例仅有效,因为您选择了表中的每隔一行。尝试选择前两行,看看会发生什么。已经说过,这不是问题的真正解决方案,因为您不应该从第一个 TableModel 中删除数据。

It's correct but when I click to First button again I get same jTable that as jTable in Second button.

问题是您无法从第一个 TableModel 中删除数据。您需要创建一个新的TableModel,然后将数据从第一个TableModel“复制”到第二个TableModel。

关于java - jTable 仅选择选中复选框的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25836284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com