Java Swing JMenu Mnemonic
I have a menu 开发者_如何学Cthat I created using JMenu. I want to assign a shortcut key Alt-F to this menu. I used setMnemonic('F') to do that, but the menu does not recognise the mnemonic.
What is the best way to troubleshoot or debug this problem ? I find that setting a break point does not help that much.
Thank you.
Code Snippet:
//higher up in variable declaration
/** Menus on the menu bar */
private JMenu uiFindMnu = new JMenu("Find");
...
//inside the constructor
// set mnemonic for the Find menu
uiFindMnu.setMnemonic('F');
Inside of a class constructor (extending JFrame):
JMenu helpmenu = new JMenu("File");
helpmenu.setMnemonic('F');
JMenuBar menubar = new JMenuBar();
menubar.add(helpmenu);
setJMenuBar(menubar);
That worked fine for me. You'll have to give some more details about your code in order for me to give a better answer. As far as troubleshooting SWING or any application GUI, one of the best recommendations I can give is to create the simplest possible scenario. I keep a bare-bones JFrame template around that I can throw simple code like this inside for testing. Once you know it works in the simplest scenario you can step back to your project and discover what other portion of your GUI is causing a conflict with this functionality.
Just out of curiosity, you don't happen to have a local variable called 'uiFindMnu' in your Constructor that is hiding your class variable, do you? I'd double check to make sure that the variable that you are calling setMnemonic() on is the one that is added to your MenuBar (and actually displayed).
Suffered with similar problem and realised that due to the setting of Look and feel after initialising the components caused the issue. Flipped the statements and it worked.
Posted a blog post here
Use setMnemonic(KeyEvent.VK_F);
I recommend you to read this about JMenus : Howto use Menus
Here is an extract of this article :
Menus support two kinds of keyboard alternatives: mnemonics and accelerators. Mnemonics offer a way to use the keyboard to navigate the menu hierarchy, increasing the accessibility of programs. Accelerators, on the other hand, offer keyboard shortcuts to bypass navigating the menu hierarchy. Mnemonics are for all users; accelerators are for power users.
A mnemonic is a key that makes an already visible menu item be chosen. For example, in MenuDemo the first menu has the mnemonic A, and its second menu item has the mnemonic B. This means that, when you run MenuDemo with the Java look and feel, pressing the Alt and A keys makes the first menu appear. While the first menu is visible, pressing the B key (with or without Alt) makes the second menu item be chosen. A menu item generally displays its mnemonic by underlining the first occurrence of the mnemonic character in the menu item's text, as the following snapshot shows. B is the mnemonic character for this menu item
You can specify a mnemonic either when constructing the menu item or with the setMnemonic method. Here are examples of setting mnemonics and accelerators:
//Setting the mnemonic when constructing a menu item:
menuItem = new JMenuItem("A text-only menu item",
KeyEvent.VK_T);
//Setting the mnemonic after creation time:
menuItem.setMnemonic(KeyEvent.VK_T);
As you can see, you set a mnemonic by specifying the KeyEvent constant corresponding to the key the user should press.
精彩评论