2015-06-23 69 views
1

我的代碼應該比較兩個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){} 
} 
+0

你會得到例外嗎?打印一下,看看是否在你期望的時候調用'deleteRow'方法。可能有助於查明問題 – Danielson

+0

我無法獲得任何例外。一些行被刪除,但不是全部 –

+0

1.爲了更好的幫助,儘快發佈一個SSCCE/MCVE,短小,可運行,可編譯的硬編碼值,用於局部變量的兩個模型,三個簡短的問題2. extends Thread {???,3.靜態???)public static void deleteRow,3。 model.fireTableDataChanged();對於DefaultTableModel? – mKorbel

回答

1

嘗試它周圍

@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); 

     } 
    } 
} 
+0

也許,但問題可以在其他地方,在MilkyWay中間的某處(基於OP的問題和代碼張貼在這裏) – mKorbel

+0

是的,但是...如果你有9行,刪除第一行,你有8行,如果你嘗試刪除第9行,你會得到一個NullPointer(取決於它如何處理)。如果你運行它,圍繞'我'項目的其他方式仍然指向正確的元素。由於我不清楚JTable本身,所以我認爲問題不在那裏 – Danielson

1

我都面臨着一定的問題,當用戶界面不會很好地響應問題之一是realetd與JTable中的數據,並在非Swing線程更新它的其他方式。所以,你可以嘗試把你的代碼在Swing線程,正是在一個工作線程

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(); 
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 
相關問題