title: What is the right action to take upon closing windows in java/swing?
body: <p>I just wrote this test code in my CustomUIPanel class:</p>
  
  <pre><code>public static void main(String[] args) {
      final JDialog dialog = CustomUIPanel.createDialog(null, 
         CustomUIPanel.selectFile());
      dialog.addWindowListener(new WindowAdapter() {
          @Override public void windowClosing(WindowEvent e) {
              System.exit(0);
          }
      });
  }
  </code></pre>
  
  <p>It works correctly if <code>CustomUIPanel.main()</code> is the program's entry point, but it makes me wonder something: what if another class called <code>CustomUIPanel.main()</code> for testing? Then my call to <code>System.exit(0)</code> is incorrect.</p>
  
  <p>Is there a way to tell the Swing event dispatch thread to exit automatically if there are no top-level windows?</p>
  
  <p>If not, what's the right thing for a JDialog/JFrame to do upon closing if the goal is for the program to exit when all the top level windows are closed?</p>