Home > OS >  How to add shortcuts to Menu (not MenuItem) in JavaFX
How to add shortcuts to Menu (not MenuItem) in JavaFX

Time:06-20

I want to do menu like this (with shortcuts):

enter image description here

So, I did:

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?

CodePudding user response:

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.).

  • Related