Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

When i try to delete the last row in Jtble it throws me an OutBound error.

here the code that implements the Jtable & DefaultTable:

Vector<String> rowOne = new Vector<>();  
        rowOne.addElement("Harry");
        rowOne.addElement("100414");
        rowOne.addElement("21");
        rowOne.addElement("239438"); 
        rowOne.addElement("24/24/23");
        rowOne.addElement("30000");

        Vector<String> rowTwo = new Vector<>(); 
        rowTwo.addElement("Gordon");
        rowTwo.addElement("34353");
        rowTwo.addElement("25");
        rowTwo.addElement("2538"); 
        rowTwo.addElement("24/24/23");
        rowTwo.addElement("20000");

        Vector<Vector> rowData = new Vector<>();
        rowData.addElement(rowOne);
        rowData.addElement(rowTwo);

        columnNames = new Vector<>();
        columnNames.addElement("Name");
        columnNames.addElement("Cc");
        columnNames.addElement("Age");
        columnNames.addElement("Phone");
        columnNames.addElement("Date");
        columnNames.addElement("Amount");


        DefaultTableModel model = new DefaultTableModel(rowData, columnNames);
        Jtable table = new JTable(model);

Here the deleting code:

else if (e.getActionCommand().equals("deleteClient"))
        {
            if(table.getSelectedRow() != -1)
            {
                DefaultTableModel tModel1 = (DefaultTableModel) table.getModel();
                int seletedRow = table.getSelectedRow();

                tModel1.removeRow(seletedRow);
            }

The error is thrown just when deleting the last Jtable's row, when I delete a diferent row as first one or a middle one, no error is thrown, how can i solve it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
143 views
Welcome To Ask or Share your Answers For Others

1 Answer

You're problem is not with the line

tModel1.removeRow(seletecdRow);

but

int selectedRow = table.getSelectedRow();
int selectedCol = table.getSelectedColumn();
tModel1.removeRow(selectedRow);
String name = (String) tModel1.getValueAt(seletecdRow, seletecdCol); // Here

When you delete the last row, the index you store in selectedRow is no longer valid. Moreover whenever you delete a row, it gets de-selected and so, table.getSelectedRow() will return -1 (as no row is selected).

Lets say you have 5 rows:

You can select any row from row 1 to row 4 (index 0 to 3) and delete it. selectedRow = 0,1,2 or 3 and you're left with 4 rows so there still exists a row at index 0, 1, 2 or 3. and model.getValueAt(selectedRow... works

But if you select row 5 (last row, at index 4) and delete it. selectedRow = 4 and you're left with 4 rows (index 0, 1, 2, 3) but there is no longer a row at index 4. and model.getValueAt(selectedRow... gives an error

The solution depends on what you're trying to do in the label after deleting the row.

  • If you're trying to set the label to display the info of the deleted row:

    String name = (String) tModel1.getValueAt(table.getSelectedRow(), table.getSelectedColumn());
    labelStatus.setText(name);
    tModel1.removeRow(table.getSelectedRow());
    
  • If you're trying to re-select another row in the table and set the label to display the info of this newly selected row:

    int selectedRow = table.getSelectedRow();
    tModel1.removeRow(selectedRow);
    int numRowsLeft = tModel1.getRowCount();
    if (numRowsLeft > 0) { // only reset selection if table is not empty
        if (selectedRow > numRowsLeft - 1) { // last row deleted, new selection will be new last row
            table.setRowSelectionInterval(numRowsLeft-1, numRowsLeft-1);
        } else { // deleted from the start or middle, selectedRow now points to the next row after the deleted row
            table.setRowSelectionInterval(selectedRow, selectedRow);
        }
        String name = (String) tModel1.getValueAt(table.getSelectedRow(), table.getSelectedColumn());
        labelStatus.setText(name);
    }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...