Window (swing) applications

How to set frame`s own (default) close operation ?

When frame started, you can change its default close operation. This example shows how to link this operation to your own method.

In this case, when user is trying to close frame, this question is shown. The window is closed definitively, when user`s answer is Yes.

Here is the code (call it for example after frame initialization):

/* code for handling window closing */

WindowListener exitListener = new WindowAdapter() {
  public void windowClosing(WindowEvent e) {
    int confirm = JOptionPane.showOptionDialog( null, "Already close application ?", 
                  "Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null );
    if (confirm == 0) {

/* set to own method */

setDefaultCloseOperation( DO_NOTHING_ON_CLOSE );