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

时间:2015-06-23 10:21:44

标签: java swing jtable

我的代码应该比较两个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){}
}

3 个答案:

答案 0 :(得分:1)

以相反的方式尝试

<div id="ajax--container">
<section class="component">
    <div class="page">
        Test
        <script type="text/javascript">
            $(document).ready(function () {
                alert(1);
            });
        </script>
    <div>
  </section>
</div>

答案 1 :(得分:1)

当UI没有响应时,我遇到了一个问题,其中一个问题是使用Jtable数据进行realetd并在非摇摆线程中更新它。因此,您可以尝试将代码放在Swing线程中,恰好在Worker线程中

SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
   @Override
   protected Void doInBackground() throws Exception {
   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);

            }
        }

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

  worker.execute();

答案 2 :(得分:0)

如果您尝试从0到长度删除,您将始终删除一半行[循环仅执行1/2次]或删除某些行(如果您已添加任何条件)。并且它不会抛出任何异常。您可以在下面的示例代码和打印语句中看到。

 //Only half will be removed loop executes n/2 times
 System.out.println("From 0 to n");
        for (int i = 0; i < model.getRowCount(); i++) {
            System.out.println("i = " + i + " rowCount = " + model.getRowCount());
            model.removeRow(i);
        }

From 0 to n
i = 0 rowCount = 16
i = 1 rowCount = 15
i = 2 rowCount = 14
i = 3 rowCount = 13
i = 4 rowCount = 12
i = 5 rowCount = 11
i = 6 rowCount = 10
i = 7 rowCount = 9
will stop here
---

//Everything will be removed loop executes n times
System.out.println("From n to 0");
    for (int i = model.getRowCount() - 1; i >= 0; i--) {
        System.out.println("i = " + i + " rowCount = " + model.getRowCount());
        model.removeRow(i);
    }

From n to 0
i = 15 rowCount = 16
i = 14 rowCount = 15
i = 13 rowCount = 14
i = 12 rowCount = 13
i = 11 rowCount = 12
i = 10 rowCount = 11
i = 9 rowCount = 10
i = 8 rowCount = 9
i = 7 rowCount = 8
i = 6 rowCount = 7
i = 5 rowCount = 6
i = 4 rowCount = 5
i = 3 rowCount = 4
i = 2 rowCount = 3
i = 1 rowCount = 2
i = 0 rowCount = 1