JFrame On Close Operation

The JAVA Noob picture The JAVA Noob · May 6, 2012 · Viewed 63.1k times · Source

I was wondering if there is a way, by clicking on the "X", to let the program perform some code before closing the JFrame. The setDefaultCloseOperation() method takes only an integer.

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Answer

MaynorSong picture MaynorSong · Nov 2, 2013
this.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e){
                    int i=JOptionPane.showConfirmDialog(null, "Seguro que quiere salir?");
                    if(i==0)
                        System.exit(0);//cierra aplicacion
                }
            });