I'm trying to remove all but the first child component from a Java Container. The following code logs "There are 3" and throws an ArrayIndexOutOfBoundsException: Array index out of range: 2
int componentCount = cardPanel.getComponentCount();
logger.info("There are " + componentCount);
for (int i = 1; i < componentCount; i++) {
cardPanel.remove(i);
}
However, this modified version works perfectly:
Component[] components = cardPanel.getComponents();
logger.info("There are " + components.length);
for (int i = 1; i < components.length; i++) {
cardPanel.remove(components[i]);
}
It appears that the Container.getComponentCount() and Container.remove(int i) can't agree on the number of components in the container. Has anyone else run into this problem?