-1

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?

Cohen
  • 79
  • 1
  • 9
  • Did you verify your index? Did you display the selected row number? Did you display the number of rows in the model? If it works for the other rows it will work for the last row as well. Here is a working example that allows you to delete 1 (or more) selected rows: http://stackoverflow.com/questions/37417494/how-do-i-make-swing-delete-an-entry-from-a-table-by-selecting-rows-and-clicking/37419226#37419226 – camickr Aug 21 '16 at 01:40
  • what do u mean with display the selected row number? – Cohen Aug 21 '16 at 01:43
  • As i mentioned It works fine for all rows excepting the last one, no sure why. – Cohen Aug 21 '16 at 01:44
  • 1
    I mean did you do any debugging of your code? How do you know you are deleting the proper row index? Post a proper [SSCCE](http://sscce.org/) that demonstrates the problem. Simplify the problem so you only have a frame containing the table and a JButton to delete the row. The link I gave you above shows how to create a simple SSCCE to demonstrate a problem. We can't tell why the code won't work based on 4 lines of code. – camickr Aug 21 '16 at 01:45
  • 1
    That is NOT a SSCCE! Did you even read my comment? Where is the frame, table and button? How can I compile and test that code??? If you want help then make an effort to give us the information we need to solve the problem. Did you look at the link I provided. I gave you working code. How is your code different than my working code. – camickr Aug 21 '16 at 02:06
  • 1
    `Whole app.` - I give up, I don't have time to spend if you are not willing to learn how to post a proper question. You where not asked for the whole app. You where asked for a SSCCE, which demonstrates the problem. You were given a link that explains what a SSCCE is. You were even given a link which shows an example of a simple SSCCE that deletes a row from a table. If you can't learn from that then I can't help you. – camickr Aug 21 '16 at 02:13
  • FYI another name for an SSCCE is a [mcve]. There are two close reasons for questions that explicitly mention 'no MCVE' and I chose one. Please include an MCVE before 4 other people vote, to avoid this being closed. And as @camickr pointed out, when people give you links, **follow the link & read it.** – Andrew Thompson Aug 21 '16 at 02:33

1 Answers1

2

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