I am trying to provide a very simple "Do you wish to save this document?" dialog upon shutdown of my application.
I have used a WindowAdapter
to do this. This works perfectly if the application is closed using any GUI action (clicking the CLOSE button, closing from the task bar, closing from the Task Manager using End Task).
However, when the user logs off or kills the application process, the JVM application receives a termination signal directly. It closes any AWT resources without user confirmation and without calling the WindowAdapter
. Using a ShutdownHook
, it is not possible to have any GUI interaction, because the AWT Event Dispatch Thread has already been stopped.
So far I have found multiple ways to work around this problem:
- Use JNI or JNA to catch the terminate signal before it reaches the AWT subsystem.
- Needs separate code for every operating system.
- Use sun.misc.SignalHandler to catch the terminate signal.
- Uses a SUN proprietary interface. Not guaranteed to work on other VM's.
- Use SWT to catch the terminate signal before it reaches the AWT subsystem.
- Well-maintained interface, but I hate pulling in SWT just to resolve this small problem.
- Save the user data to a temporary file and treat the clean-up when the program is launched again.
Is there another, better way to fix this? It seems strange there is no standard way to implement something so basic and elementary. Can't I boot up the AWT subsystem again in some way?