How to make shortcut to invisible menu item in Java

限于喜欢 提交于 2019-12-02 11:58:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!