Get the application closing event
Asked Answered
L

3

19

How to get to the application closing event?

I want to get application to save all unsaved operations or show the user a message "Are you sure about closing without saving all data?" something like this.

If this will be helpful - the application is a SingleFrameAplication using Swing.

Linhliniment answered 16/2, 2012 at 19:6 Comment(0)
A
31

You could use a shutdown hook. It works for Swing, AWT and even console applications.

Example:

Runtime.getRuntime().addShutdownHook(new Thread()
{
    @Override
    public void run()
    {
        //do your stuff
    }
});
Alkoran answered 16/2, 2012 at 19:10 Comment(2)
be cautious using roseindia links. please read balusc.blogspot.com/2008/06/what-is-it-with-roseindia.html I would reccomend docs.oracle.com/javase/7/docs/api/java/lang/… as a link insteadKongo
@Sean, thanks for the warning! I have edited the answer with a more legitimate link.Alkoran
A
3

You have to add WindowListener to your JFrame / JDialog, about closing event is there method public void windowClosing(WindowEvent e) { code example here

Avast answered 16/2, 2012 at 19:12 Comment(0)
C
0

You can use sun.misc.Signal or Runtime.getRuntime().addShutdownHook

More info

Corder answered 16/2, 2012 at 20:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.