简体   繁体   中英

Capture "quit" from dock icon in Java App

On OS X (and possibly on Windows but haven't tried yet) I need to interrupt the choice to quit the application with a confirmation. Note: This is NOT the close button on a window but choosing quit from the Dock Icon and the app menu. BTW, if they are different then I need both listeners. Overriding processWindowEvent and setting setDefaultCloseOperation() doesn't seem to work.

NOTE: I found a solution for Mac and included code for Windows from the accepted answer. See below.

Since you mentioned setDefaultCloseOoperation() I assume you are talking about JFrame .

Here is how you can do that.

this.addWindowListener(new java.awt.event.WindowAdapter() 
{
    @Override
    public void windowClosing(java.awt.event.WindowEvent windowEvent) 
    {
        int ret = JOptionPane.showConfirmDialog(MyJFrame.this, "Are you sure you want to quit?");
        if(ret == JOptionPane.YES_OPTION)
        {
            dispose();
        }
    }
});

Ok, in case anyone else is interested in this, here's what I found that works in my situation:

EDIT UPDATE: I've tested this on both Windows 10 and OSX Yosemite and it works. I've incorporated James Wierzba's code below.

Create a SEPARATE class file for the Apple quit handler (apple libraries are not included in Windows JDK):

import com.apple.eawt.AppEvent;
import com.apple.eawt.Application;
import com.apple.eawt.QuitHandler;
import com.apple.eawt.QuitResponse;

public class AppleQuitHandler {
    public static void DoAppleQuit() {
        Application a = Application.getApplication();
        a.setQuitHandler(new QuitHandler() {
            @Override
            public void handleQuitRequestWith(AppEvent.QuitEvent quitEvent, QuitResponse quitResponse) {
                int ret = JOptionPane.showConfirmDialog(null, "Are you sure");
                if (ret == JOptionPane.YES_OPTION) {
                    // Go ahead and exit
                    quitResponse.performQuit();
                } else {
                    // Return to program
                    quitResponse.cancelQuit();
                }
            }
        }
    };
}

Add a listener to the root JFrame conditionally (Windows quit):

    String OS = System.getProperty("os.name").toLowerCase();
    if(OS.contains("win")) {
        myJFrame.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                int ret = JOptionPane.showConfirmDialog(null, "Are you sure");
                if (ret == JOptionPane.YES_OPTION) {
                    songFrame.dispose();
                    System.exit(0);
                }
            }
        });
    }

Finally add the conditional for the AppleQuitHandler():

    String OS = System.getProperty("os.name").toLowerCase();
    if(OS.contains("mac")) {
        AppleQuitHandler.DoAppleQuit();
    }

You should now have a working solution for Mac and Windows to capture closing from menu's and shortcut keys.

NOTE: This is NOT thoroughly tested but I did try it on both Mac and Windows.

FYI: You will need to add the Apple libs to compile on Windows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM