How to create a shortcut to open a JMenu?

1

Talk, people know how to create a keyboard shortcuts method to open a JMenu . For example, click F1 or CTRL + "something" and open a screen.

    
asked by anonymous 31.10.2016 / 00:43

2 answers

1

Using the answer available in " How close a JFrame using keyboard events? and as per diegofm's comment in the question.

The following code opens the "jMenu1" menu by pressing the "F1" key:

KeyboardFocusManager
        .getCurrentKeyboardFocusManager()
        .addKeyEventDispatcher(new KeyEventDispatcher() {
          @Override
          public boolean dispatchKeyEvent(KeyEvent e) {
            System.out.println(e);
            if (e.getID() == e.KEY_RELEASED && e.getKeyCode() == KeyEvent.VK_F1) {
              jMenu1.setPopupMenuVisible(true);
              jMenu1.setArmed(true);
              return true;
            }
            return false;
          }
        });
    
31.10.2016 / 14:37
1

Look at this example, I think it will serve you:

JMenu actionMenu = new JMenu("Actions");
actionMenu.setMnemonic(KeyEvent.VK_A);

Where the menu will be accessed with the Alt + A

Att .

    
31.10.2016 / 00:55