So I've managed to code a "table view" with cells and all that. It works as it should so now my problem is that I don't understand why.. I have this class called WTable and it looks something like this:
The above is the important stuff from the table class. Now as you may or may not have noticed my problem is about removing cells. When removeSelectedCells() is called it calls the removeCells(List<WTableCell> cells) with the "selected" list as argument. Now notice the following on line 14:
WTableCell cell = cells.get(0);
I wanted this line to look like this:
WTableCell cell = cells.get(i);
But for some reason the cell is removed from cells (not only cells defined as a private ArrayList in the table class).
I had to create the size integer before the loop because objects are removed from "cells" even though cells.remove(Object obj) is never called within the removeCells(List<WTableCell> cells) method.
I hope that this makes somewhat sense and that someone can explain to me why the references are lost.
public class WTable extends JLayeredPane { private ArrayList<WTableCell> cells; private ArrayList<WTableCell> selected; public WTable() { cells = new ArrayList<WTableCell>(); selected = new ArrayList<WTableCell>(); } public void removeCells(List<WTableCell> cells) { int size = cells.size(); for (int i = 0; i < size; i++) { WTableCell cell = cells.get(0); removeCell(cell, (i >= size-1)); } } public void removeCell(WTableCell cell, boolean update) { boolean sel = (selected.indexOf(cell) == 0); boolean last = (cells.indexOf(cell) == cells.size()-1); cells.remove(cell); selected.remove(cell); remove(cell); cell.setTable(null); if (update) { if (!cells.isEmpty() && last) { cells.get(cells.size()-1).createImage(); } cellsDidChange(); if (sel) { Editor.cellSelectionDidChange(); } } } public void removeSelectedCells() { removeCells(selected); } }