Nathan Howard Nathan is a Java/HTML5 developer. Nathan also enjoys writing technical blog-articles and playing games in his spare time.

Do you need to process or display PDF files?

Find out why you should be using IDRSolutions software

Tutorial : How to Setup Key Combinations in JavaFX

1 min read

At IDRSolutions we have a combined Swing and JavaFX PDFViewer. In previous JavaFX articles, I have shown How to Create Stacked Menus in JavaFX and How to Create a Border Glow Effect in JavaFX. In this tutorial, I will be showing you how to set up and chain key combinations/keyboard shortcuts in JavaFX. 

We will be learning how to use the keys Cntrl + E to call a MenuItem Accelerator which will exit the application and Cntrl + C which will set the style of a Text Object. By the end of this tutorial, you should have a solid understanding of how to set up your own Key Combinations and time them to an accelerator in JavaFX.

 

Code in the Text

I will assume you have a basic understanding of JavaFX and how to set up and add to a Stage. Firstly we will create some text and pin it to the center of a BorderPane.

/**
* Create the Text.
*/BorderPane root = new BorderPane();
Text text = new Text("BEFORE");
root.setCenter(text);

 

Code in the Main Menu

We need to create some MenuItem objects to tie key accelerators/combinations to them, however, MenuItems need a Menu to contain them and a Menu has to be added to a MenuBar for our main menu to appear at the top of our application, so let’s code them in.

/**
* Create a Menu.
*/MenuBar menuBar = new MenuBar();
Menu mainMenu = new Menu("File");
MenuItem exitCmd = new MenuItem("Exit");
MenuItem textCmd = new MenuItem("Colour Text");
mainMenu.getItems().addAll(textCmd, exitCmd);
root.setTop(menuBar);

 

Code in the Listeners

We now want to add some MenuItem event handlers which will be triggered by our Key Combinations, lets code them in.

/**
* Setup MenuItem Listeners.
*///Handler to exit the application
exitCmd.setOnAction(new EventHandler() {
   @Override public void handle(ActionEvent e) {
      primaryStage.close();
   }
});
//Handler to style the text
textCmd.setOnAction(new EventHandler() {
   @Override public void handle(ActionEvent e) {
      text.setFill(Color.BLUE);
      text.setText("AFTER");
      text.setFont(Font.font("SansSerif", FontWeight.NORMAL,24));
   }
});

 

Code in the Key Combinations

Lastly, we will add our Key Combinations, when we press Cntrl + E it will Exit the application and when we press Cntrl + C it will style our text (Change the Colour and Font).

/**
* Setup KeyCombinations.
*/exitCmd.setAccelerator(new KeyCodeCombination(KeyCode.E, KeyCombination.CONTROL_DOWN));
textCmd.setAccelerator(new KeyCodeCombination(KeyCode.C, KeyCombination.CONTROL_DOWN));

 

End Result

If you have followed this tutorial correctly then see below for some before and after shots of using the Cntrl + C Key Combination, you should see something similar.

beforeafter

With some hope, you can now add your own Key Combinations to your JavaFX Applications and have learned something new! Please let us know below if you have anything to add or wish to leave a comment.

For the code/class we have written in this tutorial please see here: keyCombDemo

You may find some of my other JavaFX articles of interest :
How to Create Stacked Menus in JavaFX
How to Create a Border Glow Effect in JavaFX.

We also have a JavaFX PDF Viewer plugin for NetBeans which you can grab here.

 



Our software libraries allow you to

Convert PDF files to HTML
Use PDF Forms in a web browser
Convert PDF Documents to an image
Work with PDF Documents in Java
Read and write HEIC and other Image formats in Java
Nathan Howard Nathan is a Java/HTML5 developer. Nathan also enjoys writing technical blog-articles and playing games in his spare time.

How to insert an image into a PDF

Recently, we released JPedal 2023.07 which contains the ability to insert images into PDF files. All you need is a copy of JPedal, a...
Jacob Collins
18 sec read

6 Replies to “Tutorial : How to Setup Key Combinations in JavaFX”

  1. Helpful, but I got stuck on menu for a while – it didn’t want to appear. 🙂 Something like menuBar.getMenus().add(mainMenu) is missing – luckily easy to find in your other post you referenced. Here you name Menu “mainMenu”, in the referenced article “mainMenu” is name for the MenuBar – it also could have been consistent. Still you got me on track, thank you. 🙂

  2. Shortcut does not work when focus is on text field… What to do?
    Please help me…

  3. Thank you. This helped me. You can explain the following line because understanding this one taking extra time to me(mainly KeyCombination.CONTROL_DOWN).
    textCmd.setAccelerator(new KeyCodeCombination(KeyCode.C, KeyCombination.CONTROL_DOWN));

    You can add
    textCmd.setAccelerator(new KeyCodeCombination(KeyCode.C, KeyCombination.SHIFT_DOWN)); and can tell for this the shortkey is “shift + C”

Comments are closed.