2

Possible Duplicate:
Call a method when application closes

I want to call a method just before I close my java application using setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); I have tried WindowListener with windowClosed/windowsClosing and it doesn't work. it looks something like this

public class exampleFrame JFrame implements{

public exampleFrame (){
    this.addWindowListener(new windowAction());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

class windowAction implements WindowListener{

        @Override
        public void windowActivated(WindowEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void windowClosed(WindowEvent e) {
        System.out.prinln("window closed")

        }

        @Override
        public void windowClosing(WindowEvent e) {
            // TODO Auto-generated method stub
            System.out.prinln("window closing")
        }

        @Override
        public void windowDeactivated(WindowEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void windowDeiconified(WindowEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void windowIconified(WindowEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void windowOpened(WindowEvent e) {
            // TODO Auto-generated method stub

        }

    }

}
Community
  • 1
  • 1
user629283
  • 329
  • 1
  • 8
  • 23

1 Answers1

-1

I think what you want is something like this:

public class ExampleFrame extends JFrame{
   public ExampleFrame(){

      // you can use WindowAdapter and implement only the methods you need
      addWindowListner(new WindowAdapter(){

           @Override
           public void windowClosing(WindowEvent et) {
               System.out.println("Window closing");
           }
      }
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}