parasietje
parasietje

Reputation: 1539

Accessing AWT from ShutdownHook

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:

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?

Upvotes: 1

Views: 205

Answers (2)

trashgod
trashgod

Reputation: 205835

The host platform owns the JVM running your application. On Mac OS X you can use OSXAdapter, discussed here. Other systems may have a similar mechanism. One useful defensive measure is to use java.util.prefs.Preferences to record clean-up information. It's platform-independent and typical implementations offer best-effort retention for a given platform.

Upvotes: 2

mKorbel
mKorbel

Reputation: 109823

Maybe you complicated very simple thinks, instead of terminating current JVM instance

1) hide Container(s)

2) save applications properties,

3) on Exceptions show JOptionPane with something meaningful about

4) call System.exit(0);

5) a few biggest application (with Embedded_database for storing local data) saving these processess a few seconds after Containers hidden

0) maybe help you this thread about workaround in plain vanilla Java

Upvotes: 1

Related Questions