Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX menu item show shortcuts on right hand side

Take for example the menu items from the edit menu in JavaFX Scene Builder

See how they display the shortcuts on the right? Is there any easy way to achieve the same effect using JavaFX? Thanks.

like image 594
Nicolas Martel Avatar asked Jan 18 '14 20:01

Nicolas Martel


1 Answers

You can add an accelerator key in scene builder or add it directly in the fxml file like so

<?import javafx.scene.input.*?>
...
<MenuItem mnemonicParsing="true" onAction="#mnuSaveAction" text="%menu.title.save" fx:id="mnuSave">
   <accelerator>
      <KeyCodeCombination alt="UP" code="S" control="DOWN" meta="UP" shift="UP" shortcut="UP" />
   </accelerator>
</MenuItem>

KeyCodeCombination has two constructors, second example:

<MenuItem text="Renommer..."      onAction="#rename" mnemonicParsing="true">
   <accelerator>
      <KeyCodeCombination code="F2"><modifiers></modifiers></KeyCodeCombination>
   </accelerator>
</MenuItem>

If by "in javafx" you mean without using fxml you can use mnuSave.setAccelerator(KeyCombination);

like image 192
brian Avatar answered Sep 24 '22 02:09

brian