Workaround for incompatible JVMs not dispatching WindowClosing events

Apple JVMs on different versions of OS X have apparently been broken by the fact that they do not fire the WindowClosing event when they should (for example, if you close the main JFrame of the application by clicking the close button).

(in recent Apple Java updates, you can set a property that makes the event fire, but that's not what I'm looking for)

My problem is simple: I want to display a "prompt" when the user closes the application. However, I can't (due to the event not being generated) detect that the user has closed the window.

So, I thought I could use the stop hook:

Runtime.getRuntime().addShutdownHook(...)

      

However, apparently creating a JFrame from a locking hook seems to be problematic: it likes if the EDT is already gone after the hookdown call.

I've tried a few things and nothing really makes sense: for example, my "Tip" JFrame remains grayed out (even though it works fine when called from anywhere except the shutdown hook), or the program exits immediately. I've tried using the latch and waiting on the latch from the shutdown hook, but it's as if the EDT was no longer there.

I am currently seriously considering creating a second Java application to display the tooltip as a workaround, but I think this will be a bit outsmarted (but at least it will work).

Has anyone ever tried to create a window from the shutdown hook and call things on EDT and are there any issues to be aware of? (remember that I cannot reliably catch window close events on OS X due to known very long-standing Apple VM bugs).

0


source to share


1 answer


If the window does close and the application stops, then something calls the JFrame.dispose () method. overwrite this and add your code there.

Otherwise, you can add a daemon thread that listens for a private method in the window listener window, the daemon can add a tooltip and then dispose of the window. you can postpone the deletion until a tooltip appears.



I've never heard of this error, but now it can only get better that apple doesn't release native jdk.

+1


source







All Articles