0

Apple JVM on various OS X version have apparently been broken in that they do not generate the WindowClosing event when they should (for example if you close an app's main JFrame using by clicking on the close button).

(in the most recent Apple Java updates you can set a property forcing the event to be generated but this is not what I'm looking for)

My problem is simple: I'd like to display a "tip" when the user closes the app. However I cannot (due to the fact that no event is generated) detected that the user closed the window.

So I thought I could use a shutdown hook:

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

However apparently creating a JFrame from a shutdown hook seems problematic: it's like if the EDT was already gone once the shutdown hook is called.

I tried several things and nothing really seems to makes sense: like my "Tip" JFrame staying all gray (despite it working fine when called from anywhere but the shutdown hook) or the program exiting immediately. I tried using a latch and waiting on the latch from the shutdown hook but it's as if the EDT wasn't there anymore.

I'm currently seriously considering spawning a second Java app just to display the tooltip as a workaround but I think it's a bit overkill (but at least it would work).

Did anyone ever try to create a window from a shutdown hook and invoke things on the EDT and are there any gotchas to be aware of? (remember that I cannot reliably catch the window closing events on OS X due to known very-longstanding Apple VM bugs).

Zoyd
  • 3,449
  • 1
  • 18
  • 27
Cedric Martin
  • 5,945
  • 4
  • 34
  • 66
  • 1
    I'm not familiar with this OS X bug. Do you have a reference/link on this? – Hovercraft Full Of Eels Oct 13 '11 at 02:50
  • Ugh, the last thing I want an app to do when I close it is open something else. – Dave Newton Oct 13 '11 at 03:00
  • @Dave Newton: having a window pop-up when you close the app is very, very common. For example you may have encountered software popping-up a window asking you *"You didn't save file xxx, do you want to save it before exiting"*. – Cedric Martin Oct 13 '11 at 11:12
  • @HovercraftFullOfEels: the link's longer than the comments allowed here but *apple.eawt.quitStrategy* can do the trick in recent Apple JVMs (once again, this is *not* what I'm after). Actually the Apple JVMs were/are full of bugs. It is properly crazy. The Apple dev Java mailing lists is the place to read to learn about all these bugs and it's quite honestly frightening. As someone wrote *"things can only get better now that Apple isn't releasing its own jdk's"*. – Cedric Martin Oct 13 '11 at 11:17
  • Anything you can use [here](http://stackoverflow.com/questions/2061194/swing-on-osx-how-to-trap-command-q/2061318#2061318). – trashgod Oct 13 '11 at 13:11

1 Answers1

1

If the window is actually closing and the application is stopping then something is calling the JFrame.dispose() method. overwrite this, and add your code there.

otherwise you can add a daemon thread that listens to the closed method on the window listener, the daemon can add the tooltip and then dispose of the window. you can delay the dispose until the tooltip is done.

I've never heard of this bug, but things can only get better now that apple isnt releasing its own jdk's.

aepurniet
  • 1,719
  • 16
  • 24