how to make closing JPanel trigger a method

jpanel

Solution

frame.addWindowListener(new WindowAdapter() {
  public void windowClosing(WindowEvent e) {
    //do something
  }
});

See: http://docs.oracle.com/javase/7/docs/api/java/awt/event/WindowListener.html#windowClosing%28java.awt.event.WindowEvent%29

Problem

I got an example of JPanel from the Oracle tutorials I see it uses a default method to close the window ``` frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ``` what I want is the act of closing the window when clicking the close button, to trigger another method. How can I do that?

Original source