Home > Enterprise >  Is there a way to show KeyBindings with the JMenuBar similar to how is natively shown on MacOS?
Is there a way to show KeyBindings with the JMenuBar similar to how is natively shown on MacOS?

Time:05-17

I'm using Java Swing to try and build an application that feels as native-y on a Mac as possible. My application has a lot of key binds for the Menu actions, and I want them to display in the Menu Bar similar to how they do in Safari (see screenshot below). Any advice?

1

CodePudding user response:

See enter image description here

import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import stackoverflow.Main.TestPane;

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

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JMenuBar menuBar = new JMenuBar();
                JMenu fileMenu = new JMenu("File");
                fileMenu.setMnemonic('F');
                menuBar.add(fileMenu);

                fileMenu.add(createMenuItem("New Window", 'N', KeyStroke.getKeyStroke(KeyEvent.VK_N, KeyEvent.META_DOWN_MASK)));
                fileMenu.add(createMenuItem("New Private Window", 'W', KeyStroke.getKeyStroke(KeyEvent.VK_N, KeyEvent.META_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK)));
                fileMenu.add(createMenuItem("New Tab", 'T', KeyStroke.getKeyStroke(KeyEvent.VK_T, KeyEvent.META_DOWN_MASK)));
                fileMenu.add(createMenuItem("Open File...", 'O', KeyStroke.getKeyStroke(KeyEvent.VK_O, KeyEvent.META_DOWN_MASK)));
                fileMenu.add(createMenuItem("Open Location...", 'L', KeyStroke.getKeyStroke(KeyEvent.VK_L, KeyEvent.META_DOWN_MASK)));
                fileMenu.addSeparator();
                fileMenu.add(createMenuItem("Close Window", 'C', KeyStroke.getKeyStroke(KeyEvent.VK_W, KeyEvent.META_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK)));
                fileMenu.add(createMenuItem("Close All Windows", 'A', KeyStroke.getKeyStroke(KeyEvent.VK_W, KeyEvent.META_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK | KeyEvent.ALT_DOWN_MASK)));
                fileMenu.add(createMenuItem("Close Tab", KeyStroke.getKeyStroke(KeyEvent.VK_W, KeyEvent.META_DOWN_MASK)));
                fileMenu.add(createMenuItem("Save As...", KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.META_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK)));
                fileMenu.addSeparator();
                fileMenu.add(createMenuItem("Share"));
                fileMenu.add(createMenuItem("Export as PDF..."));
                fileMenu.addSeparator();
                fileMenu.add(createMenuItem("Impory From"));
                fileMenu.add(createMenuItem("Export Bookmarks..."));
                fileMenu.addSeparator();
                fileMenu.add(createMenuItem("Print...", 'P', KeyStroke.getKeyStroke(KeyEvent.VK_P, KeyEvent.META_DOWN_MASK)));

                JFrame frame = new JFrame();
                frame.setJMenuBar(menuBar);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public JMenuItem createMenuItem(String text, char mnemonic) {
        return createMenuItem(text, mnemonic, null);
    }

    public JMenuItem createMenuItem(String text, char mnemonic, KeyStroke keyStroke) {
        JMenuItem menuItem = new JMenuItem(text);
        menuItem.setMnemonic(mnemonic);
        menuItem.setAccelerator(keyStroke);
        return menuItem;
    }

    public JMenuItem createMenuItem(String text, KeyStroke keyStroke) {
        JMenuItem menuItem = new JMenuItem(text);
        menuItem.setAccelerator(keyStroke);
        return menuItem;
    }

    public JMenuItem createMenuItem(String text) {
        return new JMenuItem(text);
    }

    public class TestPane extends JPanel {

        public TestPane() {
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.dispose();
        }

    }
}

You can also make use of enter image description here

Desktop.getDesktop().setDefaultMenuBar(menuBar);

JFrame frame = new JFrame();
//frame.setJMenuBar(menuBar);

I would recommend becoming familiar with the Desktop API, as it will be very helpful for you on the Mac platform

  • Related