Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect the JFrame is closed?

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.

like image 701
DNB5brims Avatar asked Nov 21 '11 11:11

DNB5brims


People also ask

Which method is used to close the JFrame?

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.

How do I stop a JFrame from closing?

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.

How do you close a JFrame while opening another one?

The key is to set setDefaultCloseOperation(JFrame. DISPOSE_ON_CLOSE) .

How do you make a JFrame visible?

The String argument provides a title for the frame. To make the frame visible, invoke setVisible(true) on it.


1 Answers

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?

like image 54
vaughandroid Avatar answered Oct 17 '22 05:10

vaughandroid