user3758262
user3758262

Reputation: 21

removing items from List through JTable

I made TableModel that uses an ArrayList, I was trying to remove selected item from both table and List, I wanted it to delete all selected items, but I had exceptions thrown, so I tried to make it simple, I made this:

public void actionPerformed(ActionEvent ev) {
    purchases.remove(purchasesTable.convertRowIndexToModel(purchasesTable.getSelectedRow()));

    purchasesTableModel.fireTableDataChanged();
}

But when I remove rows, even though it works as intended sometimes I get these exceptions:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3 at java.util.ArrayList.rangeCheck(ArrayList.java:638) at java.util.ArrayList.get(ArrayList.java:414) at table.PurchasesTableModel.setValueAt(PurchasesTableModel.java:62) at javax.swing.JTable.setValueAt(JTable.java:2743) at javax.swing.JTable.editingStopped(JTable.java:4725) at javax.swing.AbstractCellEditor.fireEditingStopped(AbstractCellEditor.java:141) at javax.swing.DefaultCellEditor$EditorDelegate.stopCellEditing(DefaultCellEditor.java:368) ...

and I have no idea what can cause this

in my model in setValueAt I just do this: Purchase purchase = list.get(rowIndex); so from what I understand this function gets incorrect index, but why? ;/

Well thank you all for help ;p I FOUND SOLUTION: when I was deleting cell was still in editing mode, it was JComboBox

if (purchasesTable.isEditing()) purchasesTable.getCellEditor().stopCellEditing();

purchasesTable.editingStopped(new ChangeEvent(purchasesTable));

both of these solutions worked for me

Upvotes: 0

Views: 49

Answers (1)

Fullagar
Fullagar

Reputation: 38

The error is telling you that your index number is not an index in ArrayList.size(). A size() of 3 is 0 indexed and will have indexes [0, 1, 2]. An index of 3 is out of bounds.

Upvotes: 1

Related Questions