Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

JavaFx Default & Cancel Buttons

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:

Fig 1. Button Handler Functions
Fig 1. Button Handler Functions

Explanation

  1. 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.
  2. 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:

Fig 2. Create Buttons and Show it in the Stage
Fig 2. Create Buttons and Show it in the Stage

Explanation

  1. 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.
  2. 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.
  3. 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.
  4. 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:

Fig 3. JavaFx Button Example
Fig 3. JavaFx Button Example

Explanation

  1. 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.
  2. 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.
  3. 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.

Fig 4. Default and Cancel Buttons
Fig 4. Default and Cancel Buttons

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

Categories: JavaFx

Tags: , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.