0

I have a swing application. As long as the application runs I manipulate the within class. But when user EXITS_ON_ClOSE , I have to save the data in a file How do I save it. Here is my current implementation

    public static void main (String args[]) throws NumberFormatException, IOException{
        displayCalendar p;
        Saver save = new Saver();      
        p = new displayCalendar(save);

        JFrame fr = new JFrame();
        fr.add(p);
        fr.setDefaultCloseOperation(EXIT_ON_CLOSE);
        fr.setSize(500,500);
        fr.setVisible(true);
        save.saveAll();
}

That is I am trying to save before exiting main. But I can't get to that function at all. How do I save it?

Tamim Addari
  • 7,591
  • 9
  • 40
  • 59
  • Have a look at [How to Write Window Listeners](http://docs.oracle.com/javase/tutorial/uiswing/events/windowlistener.html) – MadProgrammer May 28 '14 at 10:42

3 Answers3

1

Here may be your solution

addWindowListener(new WindowAdapter()
        {
            @Override
            public void windowClosing(WindowEvent windowEvent)
            {
               // here your logic to save the data
                System.out.println("Window is being Closed");
                windowEvent.getWindow().dispose();
            }
        });
Macrosoft-Dev
  • 2,195
  • 1
  • 12
  • 15
0

Add WindowListener with JFrame,

fr.addWindowListener(new WindowAdapter() {
   @Override
   public void windowClosing(WindowEvent e) {
       //Call the method here.
   }
}
Masudul
  • 21,823
  • 5
  • 43
  • 58
0

Create a custom operation on setDefaultCloseOperation with WindowAdapter :

See this i hope it's helpful for you.

Community
  • 1
  • 1
Mifmif
  • 3,132
  • 18
  • 23