Swing on OSX: How to Trap command-Q?
java, macos, swing
Solution
The top voted answer is excellent but just to fill in the "best way":
System.setProperty("apple.eawt.quitStrategy", "CLOSE_ALL_WINDOWS");
This will trigger the standard window closing callback event which should work really nicely for portable code.
As a result of the discussion below it seems that its crucial to do this really early in the app. I wrote this early in the static initializer of the main class before any UI code was executed.
Problem
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?