How to detect the JFrame is closed?

12,281

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.

Check Out

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.

Share:
12,281
DNB5brims
Author by

DNB5brims

Updated on June 24, 2022

Comments

  • DNB5brims
    DNB5brims about 2 years

    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.