title: Java Swing: Do something when a component has *finished* resizing
body: <p>Apologies for the somewhat unclear question - couldn't think of a better way of putting it.</p>
  <p>I use a <a href="http://download.java.net/javadesktop/swinglabs/releases/0.8/docs/api/org/jdesktop/swingx/JXTaskPane.html" rel="nofollow noreferrer">JXTaskPane</a> (from the Swing labs extension API) to display some information.</p>
  <p>The user can &quot;click&quot; the title to expand the panel.  The JXTaskPane is in a container JPanel, which is then added to a JFrame, my main application window.</p>
  <p>I want my application window to resize to the size of the expanded task pane.  To achieve this, I added a component listener to my container JPanel which would set size to the now expanded panel.</p>
  <pre><code>panel.addComponentListener(new ComponentListener()
  {   
      public void componentResized(ComponentEvent e)
      {
          Dimension newSize = ((JXTaskPane)e.getSource()).getSize();
          reSizeFrame(newSize);
      }
  }
  
  private void reSizeFrame(Dimension newSize)
  {   
      if ((newSize.height &lt; maxSize.height) &amp;&amp; (newSize.width &lt; maxSize.width))
      {
          containerPanel.setSize(newSize);
          appFrame.setSize(containerPanel.getSize());
          appFrame.pack();
      }
  }
  </code></pre>
  <p>The problem is that the <strong>componentResized</strong> method is called as the task pane expands, as a result the resizeFrame method is called lots of times, and looks really awful on the screen.</p>
  <p>How can I detect when the JXTaskpane has finished resizing?  I thought of two approaches:</p>
  <ul>
  <li><p>Put the resizeFrame() method in a SwingUtilities.invokeLate(..) call.</p>
  </li>
  <li><p>Put in a timer resizeFrame call, so any subsequent calls do not do anything until the timer fires.  This should give enough time for the panel to resize.</p>
  </li>
  </ul>
  <p>What is the best way forward?</p>
  <p>Also - This is my first <em>serious</em> Java GUI app after years of server side program. StackOverflow has been very helpful.  So thanks!</p>