--- old/src/java.desktop/share/classes/java/awt/Container.java 2016-08-10 17:31:28.172915900 +0300 +++ new/src/java.desktop/share/classes/java/awt/Container.java 2016-08-10 17:31:27.440868300 +0300 @@ -1109,17 +1109,18 @@ } checkAddToSelf(comp); checkNotAWindow(comp); - if (thisGC != null) { - comp.checkGD(thisGC.getDevice().getIDstring()); - } - /* Reparent the component and tidy up the tree's state. */ if (comp.parent != null) { comp.parent.remove(comp); - if (index > component.size()) { - throw new IllegalArgumentException("illegal component position"); - } + if (index > component.size()) { + throw new IllegalArgumentException("illegal component position"); + } } + if (thisGC != null) { + comp.checkGD(thisGC.getDevice().getIDstring()); + } + + //index == -1 means add to the end. if (index == -1) {