How to detect the JFrame is closed?
Solution 1
I'm not sure what the situation is on Macs, but on Windows you get the windowClosing() callback from the close button; Alt-F4
; and if you close the app via task manager. You don't get the callback if you use task manager to kill the process, but I wouldn't expect that anyway.
You have remembered to call setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
on your JFrame instance, haven't you?
Solution 2
there is one more method windowClosed() try overriding thing method. hope it will work for you.
Solution 3
Sounds like you need to add some KeyListeners
and a factory to detect the one you want for a particular operating system.
Solution 4
You can use this osx library: com.apple.eawt.ApplicationListener
handleQuit(ApplicationEvent event)
Will probably do the trick.
Information from the docs:
Called when the application is sent the Quit event. This event is generated when the user selects Quit from the application menu, when the user types Command-Q, or when the user control clicks on your application icon in the Dock and chooses Quit. You can either accept or reject the request to quit.
Of course this solution will not work on Windows. As far as I know there is however no universal solution, so this is probably the best way to go.
DNB5brims
Updated on June 24, 2022Comments
-
DNB5brims about 2 years
I tried a
addWindowListener
and implement thewindowClosing
, it works, when I press the close button, but when I useCmd+Q
to close, thewindowClosing
is not being called, how can I solve it? Do I need to detectCmd+Q
on mac,Alt + F4
on windows via key listener? Is that a general listener for closing window, whatever via the close button or keyboard, or eventCtrl+Alt+Delete
orCmd+Option+Esc
to focus kill? Thanks.