Issue
I want to do menu like this (with shortcuts):
Menu fileMenu = new Menu("File");
fileMenu.setAccelerator(new KeyCodeCombination(KeyCode.F, KeyCombination.ALT_DOWN));
But it doesn't work. When I press ALT+F nothing happens. Besides, I don't know how to make key letters underlined. Can anyone say how to do it?
Solution
What you're looking for is a "mnemonic", not an accelerator. Simply put an underscore (_
) in front of the letter you want to use and ensure the mnemonicParsing
property is set to true
, which it is by default.
For example, the following:
Menu fileMenu = new Menu("_File");
Will let you use ALT+F to display the menu.
Note this works with MenuItem
as well (Menu
extends MenuItem
). Adding a mnemonic to a MenuItem
provides another way for your user to fire the item's action (in addition to clicking it, using an accelerator, etc.).
Warning: Mnemonics are not supported on all platforms (e.g., Macs). Consequently, you may want to define the accelerator as well.
Answered By - Slaw
Answer Checked By - David Marino (JavaFixing Volunteer)