1

I have an application which on exit should close all windows and write some configuration files.

Without the things I did below, the following was never triggered and so the closeEXES which writes all the configs out was never run.

    mainframe.addWindowListener(new java.awt.event.WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent winEvt) {
            closeEXES();
        }
    });

I used the following to catch the Cmd-Q on Mac OS:

import com.apple.eawt.QuitStrategy;
import com.apple.eawt.Application;

And in main:

    if (System.getProperty("os.name").equals("Mac OS X")) {
        Application.getApplication().setQuitStrategy(QuitStrategy.CLOSE_ALL_WINDOWS);

The version before that I did:

        public class MacQuitAdapter extends ApplicationAdapter {
            @Override
            public void handleQuit(ApplicationEvent e) {
                System.out.println("Using deprecated AplicationAdapter for handling Quit.");        
            }   
        }

How would I accomplish the same thing with JDK 8? The "same thing" is to make sure that when Command-Q is hit, the closing of the windows will be passed to AWT so that the windowClosing method will do what I want.

Alternatively, is there some other listener I need to sense the Command-Q? I'll keep looking but thought it was worth asking here.

Thanks.

evernoob
  • 103
  • 7
  • [used this](http://stackoverflow.com/questions/12332776/how-to-use-apples-handlequit-method) plus something else. – evernoob May 23 '14 at 16:13

1 Answers1

1

I'm sorry to answer my own question. Please let me know if this is a faux pas. I used this plus still kept the close-window part of the code. For JDK 8 this does most of what I want.

So now the code looks like this:

    mainframe.addWindowListener(new java.awt.event.WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent winEvt) {
            closeEXES();
        }
    });

    if (System.getProperty("os.name").equals("Mac OS X"))
    {
        Runtime.getRuntime().addShutdownHook(new Thread()
        {
            @Override
            public void run()
            {
                System.out.println("in : run () : shutdownHook");
                doCloses();
                System.out.println("Shutdown hook completed...");
            }
        });
    }

one method, closeEXES, prompts the user to make SURE they really meant to quit the application. The other checks a condition (have the files been closed before?) and closes them if they have not been closed.

For completeness:

public void closeEXES() {
    int n = JOptionPane.showConfirmDialog(mainframe,
            "Close EXES GUI?", "Closing EXES GUI",
            JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
    if (n == 0) {
        System.out.println("Exiting...start");
        doCloses();
        System.exit(0);
    }
}

public void doCloses() {
    if ( beenClosed )
    {
        System.out.println("Already closed all files.");
    }
    else
    {
        // ... a bunch of stuff here writing GUI screen positions to a file and
        // things like that ...

        beenClosed = true;
    }
}
Community
  • 1
  • 1
evernoob
  • 103
  • 7