简体   繁体   中英

Clearing row of data from jTable

So im trying to clear a row of data in a jTable, but when i get to the last row and clear it, it clears... but gives me an error " Row index out of range" need help please :D

    //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);
}                                             

OK, let's look at this again:

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

Seems like you are removing a row and then trying to select it. Indeed, the index is most likely out of range after you have removed it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM