I tried a addWindowListener
and implement the windowClosing
, it works, when I press the close button, but when I use Cmd+Q
to close, the windowClosing
is not being called, how can I solve it? Do I need to detect Cmd+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 event Ctrl+Alt+Delete
or Cmd+Option+Esc
to focus kill? Thanks.
JFrame is a subclass of java. awt. Window and thus inherits this method.) In this technique a WindowListener interface implentation is added to the frame, where the listener has a method, windowClosing(), that is called when the frame is closed.
jFrame. setDefaultCloseOperation(JFrame. DO_NOTHING_ON_CLOSE); That prevents the default behavior which the frame will be closed if the user clicks on the close button.
The key is to set setDefaultCloseOperation(JFrame. DISPOSE_ON_CLOSE) .
The String argument provides a title for the frame. To make the frame visible, invoke setVisible(true) on it.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With