Java 10: How to implement About, Preferences, and Quit menu items on MacOS

If you want to implement About, Preferences, and Quit handlers with Java 9 and newer on MacOS systems, this example Java source code shows how to do it:

import java.awt.*;
import javax.swing.*;

public class JavaAwtDesktop {

    public static void main(String[] args) {
        new JavaAwtDesktop();
    }

    public JavaAwtDesktop() {

        Desktop desktop = Desktop.getDesktop();

        desktop.setAboutHandler(e ->
            JOptionPane.showMessageDialog(null, "About dialog")
        );
        desktop.setPreferencesHandler(e ->
            JOptionPane.showMessageDialog(null, "Preferences dialog")
        );
        desktop.setQuitHandler((e,r) -> {
                JOptionPane.showMessageDialog(null, "Quit dialog");
                System.exit(0);
            }
        );

        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("java.awt.Desktop");
            frame.setSize(new Dimension(600, 400));
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }

}

I just tested this code on MacOS 10.12.x and Java 10, and it works as expected. When you click on the About, Preferences, and Quit menu items, the example dialogs are shown.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.