1. JavaFx Buttons – Default & Cancel
In the JavaFx framework, buttons play a crucial role in facilitating user interaction and triggering specific actions. Typically, users manipulate buttons by positioning their mouse cursor over them and clicking to initiate a desired action. It’s worth noting that a single container can contain multiple buttons, among which there can be a designated Default Button and a Cancel button. Upon pressing the Enter key on the keyboard, the default button’s handler is invoked, while pressing the Esc key triggers the handler associated with the Cancel button. To illustrate this concept, let’s consider an example where we create a JavaFx stage consisting of three buttons.
2. Button Handler Function
We will create a label which displays the button click event by the user. The handler functions make use of the setText method to report a button is clicked by the user. Code is below:
- JavaFx Label created as a member of the Application so that all the handler function can access it to report the button click. The label uses the empty constructor and hence it will not display any text initially.
- In our example, we will create three buttons and we have three simple functions for handling the click of each button.
3. Link JavaFx Button to the Handler
JavaFx button provides setOnAction method which links the button with the handler function. Now look at the code below:
- We create three JavaFx Buttons to represent Read, Write and Cancel actions. In real world, clicking the buttons will perform a meaningful task and in our example, we will simply report that a button is clicked. In the constructor, you may notice underscore letter which provides button mnemonic. For example, “_Read” label provides a underline below the letter ‘R’ when alt key is pressed by the user. The keyboard user can hit ‘Alt+R’ to invoke the button handler. Button Mnemonic will animate the button as it is clicked by the user.
- Here, the setOnAction method takes the lambda expression and accepts the function as a handler. We tied all our handler code with the buttons now.
- The HBox layout manager is collecting all our three buttons and when it lays out the control nodes on the stage, all will appear in a single row. Next, we use VBox to have two rows of UI nodes and first one is the Label and second is the HBox which is nothing but the single row of button controls. When the UI is rendering, we will see label in the top of stage and then three buttons below it.
- Finally, we display the stage by providing the VBox as the root node. The primary stage knows VBox contains a Lebel and a HBox which is the combination of three button controls.
4. Running the Example
Now the code is ready, and we can compile and run the application. Below picture shows what we can do to test the sample application:
- When we run the application, the primary stage displays a Label and three button controls. Label does not display any initial text as we used empty constructor to create it. By default, the write button is highlighted as it is first button added to the stage.
- When you hold down the Alt key in the keyboard, all the buttons show its mnemonics with an underline. For example, we will hit the Alt+R key combination to invoke the handler for the Read button.
- The label display that Read button is clicked by the user (Via the keyboard shortcut Alt+R) and shifts the focus to the Read button by highlighting it with a border color different from other buttons.
5. Default and Cancel Buttons
A stage can have one default and one cancel button. The default button is invoked by hitting the Enter button and cancel button is invoked by the Esc button. Methods setDefaultButton and setCancelButton will set the default and cancel buttons. In the below code, we are setting the Read button as default button and Cancel as the cancel button.
When the application is running, if we hit the Enter button on the keyboard, JavaFX stage will invoke the handler method for the Read button. The same way, hitting the Esc key will invoke the handler method for the Cancel button.
6. Code Reference
public class JavaFxProj extends Application
//Sample 01: Label to Report Clicked Button
public Label lblInfo = new Label();
//Sample 02: Event Handler Methods
public void WriteHandler()
lblInfo.setText("Write Button Clicked");
public void ReadHandler()
lblInfo.setText("Read Button Clicked");
public void CancelHandler()
lblInfo.setText("Cancel Button Clicked");
public void start(Stage primaryStage)
//Sample 03: Create buttons
Button btnWrite = new Button("_Write");
Button btnRead = new Button("_Read");
Button btnCancel = new Button("_Cancel");
//Sample 04: Set Handler Functions
btnWrite.setOnAction(e ->WriteHandler() );
btnRead.setOnAction(e ->ReadHandler() );
btnCancel.setOnAction(e ->CancelHandler() );
//Sample 05: Create Layout to hold the controls
HBox ButtonRow = new HBox(btnWrite, btnRead, btnCancel);
VBox root = new VBox(lblInfo, ButtonRow);
//Sample 06: Setup Scene
Scene scene = new Scene(root);
primaryStage.setTitle("JavaFx Button Example");
//Sample 07: Setup Default and Cancel Button
public static void main(String args)