Index: src/main/java/common/java/awt/Container.java =================================================================== --- src/main/java/common/java/awt/Container.java (revision 496618) +++ src/main/java/common/java/awt/Container.java (working copy) @@ -261,14 +261,9 @@ public void remove(int index) { toolkit.lockAWT(); try { - Component comp = children.get(index); - - if (layout != null) { - layout.removeLayoutComponent(comp); - } - removeFromContainer(index); - // container events are synchronous - dispatchEvent(new ContainerEvent(this, ContainerEvent.COMPONENT_REMOVED, comp)); + releaseComponent(children.remove(index), true); + } catch (ArrayIndexOutOfBoundsException e) { + // just silently ignore exception if comp is not found } finally { toolkit.unlockAWT(); } @@ -277,11 +272,13 @@ public void remove(Component comp) { toolkit.lockAWT(); try { - try { - remove(children.indexOf(comp)); - } catch (ArrayIndexOutOfBoundsException e) { - // just silently ignore exception if comp is not found + if(comp == null) { + throw new NullPointerException(); } + + if(children.remove(comp)) { + releaseComponent(comp, true); + } } finally { toolkit.unlockAWT(); } @@ -363,21 +360,25 @@ return retIndex; } - private void removeFromContainer(int index) { - Component comp = children.get(index); - + private void releaseComponent(final Component comp, final boolean fireEvent) { comp.prepare4HierarchyChange(); if (isDisplayable()) { comp.mapToDisplay(false); } + + if (layout != null) { + layout.removeLayoutComponent(comp); + } - children.remove(index); - comp.setParent(null); invalidate(); + comp.finishHierarchyChange(comp, this, HierarchyEvent.PARENT_CHANGED); - comp.finishHierarchyChange(comp, this, HierarchyEvent.PARENT_CHANGED); + if (fireEvent) { + dispatchEvent(new ContainerEvent(this, + ContainerEvent.COMPONENT_REMOVED, comp)); + } } private void addToLayout(Component comp, Object constraints) { @@ -419,7 +420,7 @@ try { addToLayout(comp, constraints); } catch (IllegalArgumentException e) { - removeFromContainer(trueIndex); + releaseComponent(comp, false); throw e; } comp.behaviour.setZOrder(index, trueIndex);