0
votes

I'm writing a code with JTable and a MySQL database and I load a table from the database into JTable. Also I made button column, this button delete the row in which it is located. After pressing the row disappears but in database removes wrong row.

Also when I try to remove last row I get this exception:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 8 >= 8.

Here is the ActionListener method:

jButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            int row = table.convertRowIndexToModel(table.getEditingRow());
            model.removeRow(row);
            System.out.print(row);
            deleteOrder.removeFromDB(table.getModel().getValueAt(row, 0).toString());//table.getValueAt(row, column).toString());//?????????
            fireEditingStopped();
        }       
}); 

Here is the method from other class, here I load value to JTable

private static void loadOrdersTable() throws SQLException {
    String[][]data = new String[takeOrder.countOrder()][5];
    String[] columnNames = {"Order ID", "Table ID","Name","Price","Delete Button"};
    data = takeOrder.getOrder();
    DefaultTableModel model = new DefaultTableModel(data, columnNames);
    table = new JTable( model );
    table.getColumnModel().getColumn(4).setCellRenderer(new ButtonTableRenderer());
    table.getColumnModel().getColumn(4).setCellEditor(new ButtonTableEditor(new JTextField(),table, model));    
}

And here is the method where I select values from DB (TakeOrder class):

public String[][] getOrder() throws SQLException {
    int i = 0;
    String data[][] = new String[countOrder()][5];
    String query = "SELECT order_id, table_id, dish_name, dish_price FROM orders";

    try {
        statement = makeConnection.getConnection().createStatement();
        resultSet = statement.executeQuery(query);
        while(resultSet.next()) {
             String orderId = resultSet.getString(1);
             String restTableId = resultSet.getString(2);
             String name = resultSet.getString(3);
             int price = resultSet.getInt(4);
             data[i][0] = orderId;
             data[i][1] = restTableId;
             data[i][2] = name;
             data[i][3] = String.valueOf(price);
             data[i][4] = "Delete";
             i++;
        }
        System.out.print("Ilość: " + i);
    } catch(Exception e) {
        System.out.print("getOrder(String tableId) error " + e.getMessage());
    }

    return data;
}

Do you know where the problem could be?

1
The problem could be where the StackTrace says it is... - George Z.

1 Answers

0
votes

when I try to remove last row I get this exception:

int row = table.convertRowIndexToModel(table.getEditingRow());
model.removeRow(row);
System.out.print(row);
deleteOrder.removeFromDB(table.getModel().getValueAt(row, 0).toString());

Yes, you have already deleted the last row from the TableModel.

So you can't use the getValueAt(...) method to access that row again.

You need to reorder your logic. Something like:

String valueFromTable = model.getValueAt(row, 0).toString();
model.removeRow(row);
deleteOrder.removeFromDB( valueFromTable );