title: Swing addWindowFocusListener
body: <p>I have simple Java Swing application. I want before close main window get confirmation from user.</p>
  
  <p>There is my code:</p>
  
  <pre><code>package client_interface;
  
  import java.awt.EventQueue;
  import java.awt.GraphicsDevice;
  import java.awt.GraphicsEnvironment;
  import java.awt.Rectangle;
  import java.awt.event.WindowAdapter;
  import java.awt.event.WindowEvent;
  import javax.swing.JFrame;
  import javax.swing.JOptionPane;
  
  public class MainWindow {
  
      private JFrame frame;
  
      public static void main(String[] args) {
          EventQueue.invokeLater(new Runnable() {
              public void run() {
                  try {
                      MainWindow window = new MainWindow();
                      window.frame.setVisible(true);
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
          });
      }
  
      public MainWindow() {
          initialize();
      }
  
      private void setFrameSize(JFrame frame) {
          GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
          int width = gd.getDisplayMode().getWidth();
          int height = gd.getDisplayMode().getHeight();
          frame.setBounds(new Rectangle(width/4, height/4, width/2, height/2));
          //frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
      }
  
  
      private void initialize() {
          frame = new JFrame("Test");
          setFrameSize(frame);
          frame.addWindowFocusListener(new WindowAdapter() {
              public void windowClosing(WindowEvent e) {
                  if (JOptionPane.showConfirmDialog(frame, 
                          "Are you sure to close this window?", "Really Closing?", 
                          JOptionPane.YES_NO_OPTION,
                          JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION){
                          System.exit(0);
                      }
              }
          });
  
  
      }
  
  }
  </code></pre>
  
  <p>But seems that frame.addWindowFocusListener doesn't work.</p>
  
  <p>Please show me the correct way to add event windowClosing to my frame.</p>