views:

564

answers:

5

Well behaved windows programs need to allow users to save their work when they are shutting the PC down.

How can I make my app detect the shutdown event? Any solution should allow the user to abort the shutdown if user selects, say "Cancel".

The normal Swing window closing hook doesn't work, nor does adding a shutdown hook.

On testing, the methods of WindowListener (windowClosing,windowClosed, etc) do not get called.

The answer I have accepted requires the use of platform specific code (JNI to register for WM_QUERYENDSESSION ). Isn't this a bug on Swing?

See http://forums.sun.com/thread.jspa?threadID=481807&messageID=2246870

+1  A: 

how-do-i-get-my-java-application-to-shutdown-nicely-in-windows

That might be of help

PintSizedCat
A: 

The above seems to be the better answer.

I can't find any good information on detecting window shutdown events. I guess the best possible method would be to detect weather your application is trying to close, using a window closing event or the like then ask the question.

http://www.javalobby.org/java/forums/t17933

Cory
A: 

Look for signal handling in java. when Windows closes it will send a signal to the application asking it to terminate most likely a sigterm

see here for more about this (I am not the owner of the website)

Jean
+1  A: 

Write some JNI code to WM_QUERYENDSESSION message. You can get details for this from the MSDN documentation or by googling it.

If you don't want to write too much C++ code to do this I can recommend the JNA library (https://jna.dev.java.net/) which gives you some nice Java abstractions for C code.

Aidos
A: 

Hi, This is Vinee and I have the similar requirement as discussed above. We have a swing application and we want to present a confirmation box to user before the application is closed on windows shutdown and if the user selects cancel, the shutdown process should abort. Would you mind giving an update if you have got success on this issue.

Your urgent help is highly appreciated.

Thanks, Vinee

vinee
Hey Vinee - how about reading some of the answers, and trying what they say?
Stephen C