title: Run function on JFrame close
body: <pre><code>void terminate() {}
  protected JFrame frame = new JFrame();
  </code></pre>
  
  <p>How can I get <code>frame</code> to run the terminate function when I press the close button?</p>
  
  <p>Edit: I tried to run this, but for some reason it doesn't print test (however, the program closes). Does anyone have an idea what could be the problem?</p>
  
  <pre><code>frame.addWindowListener(new WindowAdapter() {
      public void WindowClosing(WindowEvent e) {
          System.out.println("test");
          frame.dispose();
      }
  });
  </code></pre>