3

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 couldn't get a concrete answer. If anybody has successfully implemented this requirement, please guide me for implementing the same.

halfer
  • 18,701
  • 13
  • 79
  • 158
vinee
  • 39
  • 2
  • 1
    Sounds tricky, because the shutdown signal goes to the JVM, that would have to make it avaible to threads running on that VM... – Andreas Dolk Jan 16 '10 at 10:39

2 Answers2

2

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.

Community
  • 1
  • 1
Stephen C
  • 632,615
  • 86
  • 730
  • 1,096
  • 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 Dolk Jan 16 '10 at 10:57
  • 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 Dolk Jan 16 '10 at 11:08
0

add this line in the event listener of button,

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

  • Yes, but the real problem is to catch the Windows shutdown event from a Swing 'application' (thread) – Andreas Dolk Jan 16 '10 at 11:01
  • 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>? – professionalcoder2010 Jan 16 '10 at 11:07
  • Do you have any working example that can identify whether the shutdown event has been triggered? – vinee Jan 16 '10 at 11:33
  • 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 Jan 16 '10 at 11:38