Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 15: JavaFX Fundamentals > 15-13. Associating Keyboard Sequences to Appl...

15-13. Associating Keyboard Sequences to Applications

Problem

You want to create keyboard shortcuts for menu options.

Solution

Create an application that will use JavaFX's key combination APIs. The main classes you will be using are shown here:

  • javafx.scene.input.KeyCode
  • javafx.scene.input.KeyCodeCombination
  • javafx.scene.input.KeyCombination

The following source code listing is an application that displays the available keyboard shortcuts that are bound to menu items. When the user performs a keyboard shortcut the application will display the key combination on the screen:

        primaryStage.setTitle("Chapter 15-13 Associating Keyboard Sequences");
        Group root = new Group();
        Scene scene = new Scene(root, 530, 300, Color.WHITE);

        final StringProperty statusProperty = new SimpleStringProperty();

        InnerShadow iShadow = InnerShadowBuilder.create()
                .offsetX(3.5f)
                .offsetY(3.5f)
                .build();
        final Text status = TextBuilder.create()
            .effect(iShadow)
            .x(100)
            .y(50)
            .fill(Color.LIME)
            .font(Font.font(null, FontWeight.BOLD, 35))
            .translateY(50)
            .build();
        status.textProperty().bind(statusProperty);
        statusProperty.set("Keyboard Shortcuts \nCtrl-N, \nCtrl-S, \nCtrl-X");
        root.getChildren().add(status);

        MenuBar menuBar = new MenuBar();

  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


 Â