How to save application options before exit?
Asked Answered
T

4

21

I have made an application and i need to save some options before exit.(something like window dimension, ..., that will be written in a file.)

The main frame has set this:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

How can I save options that interests me?(before exiting of course)

Thanks!

Tengler answered 2/3, 2010 at 7:32 Comment(0)
B
37

If you just want to do something when the application is shutting down, you can hook the shutdown using this code:

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

        public void run() {
            // Do what you want when the application is stopping
        }
    }));

However, this will not allow you to not close your window. If you need to check something before really exiting, you can override the windowClosing event:

addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
        // Do what you want when the window is closing.
    }
});

Note that the first solution - using the shutdown hook - has the advantage that it is not related to a window event, and will be executed even if the application is stopped by another event (except, of course, if the Java process is killed brutally).

Bongbongo answered 2/3, 2010 at 7:46 Comment(4)
When I used the constructor Thread() a file which was saved in the Thread.run() method was corrupted. But when I used the constructor Thread(Runnable) as suggested in this example the file was saved properly. Was I lucky or does the constructor type matter?Rachal
This is also very useful in Processing applets. When quitting them, void stop() is not always called whereas this method is. Thanks! :DQuinary
Except when you are using the Processing IDE and quit a sketch via the 'stop' button, in which case not even this method seems to work.Newfashioned
Yes @rednoyz, it will not work if you shutdown your computer too ;o) In such cases, you don't have any solution to catch the shutdown event...Bongbongo
S
7

May be the following will help.

  1. First u need to read your property file. See doc

    Properties properties = new Properties();
    try {
      properties.load(new FileInputStream("filename.properties"));
    } catch (IOException e) {
      System.err.println("Ooops!");
    }
    
  2. Second add event handler to your window, which will save your data in property file.All that you need to save just put in properties instance and store it on exit

     addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        try {
         properties.store(new FileOutputStream("filename.properties"), null);
        } catch (IOException e) {
        }
      }
    });
    

That's all, if I'd understood you correct)

Silvas answered 2/3, 2010 at 7:45 Comment(2)
Please never have empty catch-blocks, not even in example code. Put at least a e.printStackTrace() in there. Example code has the nasty tendency to become production code and we all have to maintain that later on.Zizith
Actually this was not mine), just fast copy/past. But you're absolutely right.Silvas
I
2

You may register a WindowListener and save your options in the windowClose method.

And of course

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Iniquitous answered 2/3, 2010 at 7:35 Comment(0)
B
0

Not sure this applies to your situation, but it may to others who surf to this thread. My application has a main() and at the end has the while (!shell.isDisposed) loop. When the application runs that loop is going, and when the app is closed that loop ends and it drops to what ever is after it in the main method. This is where I put a check for if the editor is dirty and if the preferences have changed and handle those to conditions. My method attached to the file close disposes the shell and calls the closeCurrent() method but if someone clicks the 'x' close window icon it falls to here and I check again if anything is dirty and save if I need to.

while (!shlCpbtool.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    // exit
    if(dirty){
        closeCurrent();
    }
Blackwood answered 31/8, 2012 at 14:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.