2013-10-23 89 views
0

所以即時嘗試清除jTable中的一行數據,但是當我到達最後一行並清除它時,它會清除......但給我一個錯誤「行索引超出範圍「需要幫助請:d從jTable清除數據行

//Get table model. 
    DefaultTableModel model = (DefaultTableModel) tblStock.getModel(); 
    //Create variable. 
    int selectedProduct = tblStock.getSelectedRow(); 
    //Remove selected row. 
    model.removeRow(selectedProduct); 
    //Clear selection. 
    tblStock.clearSelection(); 
    //Set intervals. 
    tblStock.setRowSelectionInterval(0, 0); 
    tblStock.setRowSelectionInterval(selectedProduct, selectedProduct); 
}            

回答

2

OK,讓我們再看看這個:

int selectedProduct = tblStock.getSelectedRow(); 
model.removeRow(selectedProduct); 
tblStock.setRowSelectionInterval(selectedProduct, selectedProduct); 

好像要卸下行,然後試圖將其選中。事實上,在你刪除它之後,該指數很有可能超出範圍。