문제

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.

도움이 되었습니까?

해결책

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

다른 팁

If your are not using fxml file then you can add key combination like below.

MenuItem gotoLine = new MenuItem("Goto");
KeyCombination gotoKeyCombination = new KeyCodeCombination(KeyCode.G, KeyCombination.CONTROL_DOWN);
gotoLine.setAccelerator(gotoKeyCombination);
gotoLine.setOnAction(event -> {
            System.out.println("CTRL+G event triggered");
            System.out.println(event.toString());
 });
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top