问题
I've recently added a console dialog to my program to make catching bugs easier. This is currently accessible through a JMenuItem and has keyboard shortcut 'Ctrl+L'.
Ideally I'd like to keep this option in but make the menu item hidden/invisible, because I only really want people to use it if they are copying the contents to me so I can investigate something. However, when I set the menu item to be invisible the shortcut also stops working.
Here's what I've tried:
mntmViewLogs.setAccelerator(KeyStroke.getKeyStroke('L', KeyEvent.CTRL_DOWN_MASK));
mntmViewLogs.setVisible(false);
Thanks!
回答1:
You should use the key bindings API: http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html
Set accelerator key not to menu, but to the action behind the menu: getInputMap(WHEN_IN_FOCUSED_WINDOW).put( acceleratorKey, actionname);
来源:https://stackoverflow.com/questions/19492041/how-to-make-shortcut-to-invisible-menu-item-in-java