title: Get around IndexOutOfBoundsException in java.awt.Container.createHierarchyEvents
body: <p>I have the following problem. I need to use some 3rd-party Swing-Components that I cannot change and have no influence on. However, in our situation one UI-component causes the following problem, after which it is no longer usable:</p>
  
  <pre><code>    Uncaught exception in thread Thread[AWT-EventQueue-0,6,main]: 
       java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
          at java.util.ArrayList.rangeCheck(ArrayList.java:653) ~[na:1.8.0_20]
          at java.util.ArrayList.get(ArrayList.java:429) ~[na:1.8.0_20]
          at java.awt.Container.createHierarchyEvents(Container.java:1439) ~[na:1.8.0_20]
          at java.awt.Container.createHierarchyEvents(Container.java:1439) ~[na:1.8.0_20]
          at java.awt.Container.createHierarchyEvents(Container.java:1439) ~[na:1.8.0_20]
          at java.awt.Container.createHierarchyEvents(Container.java:1439) ~[na:1.8.0_20]
          at java.awt.Component.hide(Component.java:1701) ~[na:1.8.0_20]
          at java.awt.Window.hide(Window.java:1117) ~[na:1.8.0_20]
          at javax.swing.Popup.hide(Popup.java:125) ~[na:1.8.0_20]
          at javax.swing.PopupFactory$HeavyWeightPopup.hide(PopupFactory.java:474) ~[na:1.8.0_20]
          at javax.swing.JPopupMenu.setVisible(JPopupMenu.java:796) ~[na:1.8.0_20]
          at some.3rd.party.library.Component
  </code></pre>
  
  <p><em>Edit:</em>
  Funny enough, the component does only cause this exception if we add a <code>HierarchyEventListener</code> to the Toolkit (via <code>Toolkit.getDefaultToolkit().addAWTEventListener(ours, AWTEvent.HIERARCHY_EVENT_MASK);</code>) ... And it seems that this is a rare use case. Otherwise the code in <code>java.awt.Container</code> that throws this exception does not get called at all!</p>
  
  <p>Some Internet research indicates that the underlying cause is that the component is modified outside the EventQueue-Thread (from some code within the component!), but I think I can exclude the possibility. </p>
  
  <p>Since I cannot access the code of the UI-component (and anyway have no idea where the problem gets caused), my proposed solution was to use java bootclasspath-option or javaagent technology to modify the <code>java.awt.Container</code> class and essentially surround the <code>createHierarchyEvents</code> with a try-catch-block. Honestly I don't care about the exception, as long as the component can still be used.</p>
  
  <p><em>Edit2:</em> Problem occurs on Win and Mac and the L&amp;F doesn't seem to make a difference either. However, the problem is non-deterministic, meaning that it sometimes occurs at the second or third attempt.</p>
  
  <p>Any ideas on that? Suggestions, how to proceed? Is this a good idea? Alternatives? Thanks for any input!</p>