gpt4 book ai didi

java - 如何根据行中的某些文本删除 JTable 中的行

转载 作者:行者123 更新时间:2023-11-30 03:22:32 26 4
gpt4 key购买 nike

我的代码应该比较两个 JTable 并在其中一个单元格中搜索特定单词。如果找到该词,则应删除该行。问题是,并非所有行都被删除。用户必须按几次删除按钮才能删除所有行。

 public class DeleteCleareadTable1Rows extends Thread {

@Override
public void run() {
for (int i = 0; i < table1.getRowCount(); i++) {
int modelIndex = table1.convertRowIndexToModel(i);
String status = table1.getModel().getValueAt(modelIndex, 9).toString();
if (status.equalsIgnoreCase("Cleared")) {
deleteRow(table1, table1Model, i);

}
}
}
}


public static void deleteRow(JTable table, DefaultTableModel model, int rowNo) {
try{
model.removeRow(rowNo);
model.fireTableDataChanged();
table.setModel(model);
}catch(ArrayIndexOutOfBoundsException |NullPointerException a){}
}

最佳答案

换个方式试试

@Override
public void run() {
for (int i = table1.getRowCount()-1; i >= 0; i--) {
int modelIndex = table1.convertRowIndexToModel(i);
String status = table1.getModel().getValueAt(modelIndex, 9).toString();
if (status.equalsIgnoreCase("Cleared")) {
deleteRow(table1, table1Model, i);

}
}
}

关于java - 如何根据行中的某些文本删除 JTable 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31000164/

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