After being convinced ("schooled") that Swing apps on Mac do look native, I'm trying to make mine look as native as possible. Everything looks great, but when I hit command-Q or do it from the menu, my windowStateChanged(WindowEvent e)
is not firing on my main JFrame (if I exit in any other way, it does fire). How can I respond to the real Apple quit?
views:
322answers:
3Have you tried setting up command-Q
as an accelerator in your menu? Can you make your app respond to it?
I'm not positive, but I think this works in Linux and probably Windows with the equivalent Alt-F4. My app responds to the "killing" keystroke, I process some cleanup code and then I do a programmatic System.exit()
.
If you're "just" after graceful exit handling, you may also want to catch the WindowEvent
WINDOW_CLOSING
, where traditionally "are you sure?" stuff gets done.
You can implement com.apple.eawt.ApplicationListener
and respond to the Quit
event. An example may be found in the Mac OS X Reference Library example, OSXAdapter.
This is a pretty good question, and I must admit I don't have the answer. However, a couple years ago when I was working on a Java app and faced this problem, I solved it by registering a shutdown hook with the runtime that would do what I wanted the app to do before quitting. It's a heavy-handed solution but it worked. You can take a look at my code and see if it helps.