Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

java - How to save application options before exit?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...