java - Removing Items from an ArrayList through a JTable -
i have jtable
includes items arraylist
, whenever try remove contents of deleted row arraylist
indexoutofboundsexception
depending on amount of rows wanted delete , location. how go solving issue?
runnable code: http://pastebin.com/nnrnxzdg
remove.addactionlistener( e -> { int k = 0; int[] rows = table.getselectedrows(); tablemodel tm= table.getmodel(); while(rows.length>0) { while(k<rows.length) { al.remove(table.getselectedrow() + k); k++; } ((defaulttablemodel)tm).removerow(table.convertrowindextomodel(rows[0])); rows = table.getselectedrows(); } table.clearselection(); });
basically, moment remove row, indices change. need do, make copy of selected rows, not index, actual row value...
jtable table = gettable(); if (table.getselectedrowcount() > 0) { list<vector> selectedrows = new arraylist<>(25); defaulttablemodel model = getmodel(); vector rowdata = model.getdatavector(); (int row : table.getselectedrows()) { int modelrow = table.convertrowindextomodel(row); vector rowvalue = (vector) rowdata.get(modelrow); selectedrows.add(rowvalue); }
now this, can calculate indexof
given object model , remove it...
(vector rowvalue : selectedrows) { int rowindex = rowdata.indexof(rowvalue); model.removerow(rowindex); } }
without caring index of value was.