title: Java Swing application lifecycle problem
body: <p>I have a Java application which is used to start a Swing user interface. The interface is a class with an encapsulated JFrame instance. The problem is that the application allocates some resources and the Swing interface uses these resources, but the application closes the resource <em>not</em> the user interface.</p>
  
  <p>How can I achieve either the main application gets a notification when the complete Swing interface is closed or that the start of the Swing interface blocks until it's closed. Closed means that the <a href="http://download.oracle.com/javase/6/docs/api/java/awt/event/WindowAdapter.html#windowClosed" rel="nofollow noreferrer"><code>WindowAdapter.windowClosed(WindowEvent)</code></a> method of the JFrame WindowListener was already invoked.</p>
  
  <p>The solution from this thread (<a href="https://stackoverflow.com/questions/1341699/how-do-i-make-a-thread-wait-for-jframe-to-close-in-java">link</a>) seems to return when the JFrame will be invisble, does this include the <code>WINDOW_CLOSED</code> event handling?</p>
  
  <p><strong>Edit:</strong> Maybe it will be a solution to implement this lifecycle interface:</p>
  
  <pre><code>public interface Lifecycle {
  
      public void startup();
  
      public void shutdown();
  
  }
  </code></pre>
  
  <p>Now the Swing interface class has to invoke the <code>shutdown()</code> method of the main application in the handler of the <a href="http://download.oracle.com/javase/6/docs/api/java/awt/event/WindowEvent.html#WINDOW_CLOSED" rel="nofollow noreferrer">WindowEvent.WINDOW_CLOSED</a> event.</p>
  
  <p>Is it possible and a good pratice to do so?</p>