title: Calling function on windows close
body: <p>Using Java: I have a GUI built using the netbeans GUI builder.</p>
  
  <p>The GUI class was created by extending a jFrame</p>
  
  <p><code>public class ArduinoGUI extends javax.swing.JFrame</code></p>
  
  <p>and the GUI displayed using:</p>
  
  <pre><code>java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {                    
          new ArduinoGUI().setVisible(true);                    
      }
  }
  </code></pre>
  
  <p>Therefore I don't have an actual frame object on which to call <code>frame.</code>, so how in this case can I override the <code>windowClosed</code> function, because I have to call a specific function to tidy up a serial connection before the app exits.</p>
  
  <p>Edit: here is the code explicit as answered below:</p>
  
  <pre><code>@Override
  public void processWindowEvent(WindowEvent e) {
      if (e.getID() == WindowEvent.WINDOW_CLOSING) {
          arduino.close();
          System.out.println("Arduino Close()");
          dispose();
      }
  </code></pre>