tags:

views:

124

answers:

2

I have a swing application and I 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.

I went through couple of discussions in the forum but couldnt get a concrete answer. If anybody has successfully implemented this requirement, please guide me for implementing the same.

Your urgent help is highly appreciated.

Thanks, Vinee

+1  A: 

This question discusses how to cancel a Windows shutdown programatically using the Win32 API, and gives a really simple answer. Maybe ... all you need to do is to write some JNI / JNA code and you can do the same thing from Java.

Stephen C
According to http://www.personalmicrocosms.com/pages/blog.aspx?b=64 it works with SWT/JFace. Different technology but open source and looking at SWT/JFace might help implementing something similiar for use with plain Java or Swing.
Andreas_D
And to make it more complicated: looks like if the JVM is started from a console (with cmd.exe or from a batch file), the shutdown event is sent to the console and not to the JVM... So make sure to use the ms dos start command to run the JVM
Andreas_D
A: 

add this line in the event listener of button,

Runtime.getRuntime().exec("shutdown -a");

Senthil
Yes, but the real problem is to catch the Windows shutdown event from a Swing 'application' (thread)
Andreas_D
Look at the question line clearly, "if the user selects cancel, the shutdown process should abort."he can use the code I given to abort the shutdown, though I did not give the code to catch the event of shuttingdown the system.What I given is not a complete solution, though he can use this technique... Am I right>?
Senthil
Do you have any working example that can identify whether the shutdown event has been triggered?
vinee
There are two requirements lies under the question.1. When my swing pplication is running and the user tries to shutdown, the swing application should be able to catch the window shutdown event. How to do this?2. The second part comes when the application sucessfully trap the window shutdown event. In this case the user will be presented with a dialog to either continue or cancel the shutdown. In this case if the user press the cancel button, how can I stop the shutdown process.
vinee