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