23

So i noticed that in awt there is a MenuItem constructor for adding a CTRL + (some key) shortcut, but there is no such constructor for JMenuItem. What is the correct way to do this?

I need an equivelent of awt:

MenuItem mi = new MenuItem("Copy", new MenuShortcut(KeyEvent.VK_C));

but for Swing.

Adrian Hristov
  • 1,957
  • 2
  • 16
  • 22
  • [More information from the trails](http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html#mnemonic) – Brian Nov 13 '12 at 19:08

2 Answers2

39

Example for CTRL + N.

menuItem.setAccelerator(KeyStroke.getKeyStroke('N', Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx()));

Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx() returns control key (ctrl) on Windows and linux, and command key (⌘) on Mac OS.

Anonymous
  • 4,617
  • 9
  • 48
  • 61
Dan D.
  • 32,246
  • 5
  • 63
  • 79
10

Simply create a KeyStroke and call setAccelerator(...) on the JMenuItem like so:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import java.awt.Toolkit; 

public class Test {

    public Test() {
        initComponents();
    }

    public static void main(String[] args) {
        //create Swing components on EDT
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test();
            }
        });
    }

    private void initComponents() {
        //create JFrame
        JFrame frame = new JFrame("Accelerator Sample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JMenuBar menuBar = new JMenuBar();//create menu bar to hold menus
        JMenu menu = new JMenu("File");//create a menu
        menuBar.add(menu);//add menu to bar

        JMenuItem menuItem = new JMenuItem("Say Hello");//create menu item

        //set shortcut CTRL+H (command+h on mac os)
        KeyStroke ctrlH = KeyStroke.getKeyStroke(KeyEvent.VK_H, Toolkit.getDefaultToolkit ().getMenuShortcutKeyMask());

        //set the accelerator
        menuItem.setAccelerator(ctrlH);
        //add listener which will be called when shortcut is pressed
        menuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                System.out.println("Hello, World");
            }
        });

        menu.add(menuItem);//add item to menu 'File'

        frame.setJMenuBar(menuBar);//set menubar of JFrame
        frame.pack();
        frame.setVisible(true);//set frame visible
    }
}
kritzikratzi
  • 19,662
  • 1
  • 29
  • 40
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • 1
    Nice to see the whole thing. Thanks for taking the time :). – Adrian Hristov Nov 13 '12 at 19:13
  • @AdrianHristov its a pleasure glad to be of help – David Kroukamp Nov 13 '12 at 19:13
  • am having a problem when using this on a `JMenuItem` in a `JPopupMenu` it wont work unless i press on the `JPopupMenu` and the dropdown of options appears and then while its shown it will work how can i go make the shortcut work without the need to press and show scenario i dont want to use `rootPane.registerKeyboardAction(` is that even possible – shareef Mar 11 '15 at 14:12