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 Label with Mnemonic

1. JavaFx Label Control

The JavaFx Label control is defined in the package javafx.scene.control. A Label, as the name implies, is just a display control that is used to identify or describe another UI element on a screen. JavaFx Label can show either text or an icon, or both.

A Label can be placed before the other UI node or on top of the UI node. A Label cannot be focused on. This means, we cannot use the Tab key to set the focus to a Label. Even though it will not receive focus, it can transfer the focus to other controls and in this example, we will set Mnemonic to a label control so that it will transfer the focus to another control.

2. About the Example

The below screen shows the example we are going to create. Here, we have two label controls placed before the TextBox. When user presses the Alt+P, JavaFx stage will set the focus to the TextBox next to the Person Name label. The same way, Alt+A will set the focus to the second text box control.

Fig 1. JavaFx Label Control Mnemonic
Fig 1. JavaFx Label Control Mnemonic

3. JavaFx Label Mnemonic

When creating the Label, we can pass a string to the constructor to display it as a text on the screen. In code snippet 1, we created two labels, and you can notice an underscore in the text string. For the first label, letter, P will act as a Mnemonic for the label and for the second label, A will act as a Mnemonic. The underscore is the indicator to the parsing engine to tell which letter will act as a Mnemonic. If we have more than one underscore, the letter which is next to the very first underscore acts as a Mnemonic letter. In the second part of the code snippet, we create two TextField controls which will take user input.

Fig 2. Create JavaFx Labels
Fig 2. Create JavaFx Labels

4. Link JavaFx Label with TextField

The LabelFor property will associate a JavaFx Label control with other UI Node elements. Setting this property is useful for the Mnemonics and Accelerator parsing at runtime. In the below code snippet, we linked our labels with the text box controls. For example, we linked the lblAge label control with the text field tfAge. Since the Label has a Mnemonic Alt+A, the key stroke will set focus to the Age Text Field. The same way, we associate the Person Name label with the Person Name text field.

Fig 3. Link JavaFx Label with Text Field
Fig 3. Link JavaFx Label with Text Field

5. Enable Mnemonic Parsing

Toggle the MnemonicParsing property to enable or disable text parsing. If this is set to true, the Label text will be processed to determine if it contains the mnemonic parsing character ‘_’. When a mnemonic is identified, the key combination is determined based on the next character, and the mnemonic is appended. In our example, the Mnemonics are Alt+P, Alt+A. Below code enables the Mnemonic parsing for both the labels by calling the setMenmonicParsing method.

Fig 4. Label Shortcut Key
Fig 4. Label Shortcut Key

6. Setup the Stage & Display the Controls

Now the JavaFx Labels and Text fields UI nodes are ready, and we even linked the labels with the text fields. It is time to add the controls to the JavaFx Stage. Code is below:

Fig 5. Set The Stage with Controls
Fig 5. Set The Stage with Controls

Explanation

  1. The reference variable gPane holds the GridPane instance. A GridPane can keep the controls in row and column of grids and hence it has the name,
  2. Using the addRow method, we fill the grid pane with the controls. This method takes index number of the row as a first parameter. The remaining parameters are controls and we can add any number of controls. In our example, we filled the first row with Person Name label and Text Field. In the second row, we packed the Age Label and TextField.
  3. Once the Grid Pane is ready with the controls, we construct the scene out of it. After setting a suitable title for the stage, we display it by calling the show method.

7. Testing the JavaFx Label Mnemonic

Now, we can run the sample and hit the Alt+P button. This will set the focus to the first text field control on the stage. When we hit Alt+A, the focus shifts to the second text field. Even though the Mnemonics are tied to the Label controls, the focus is set to the text fields as we already linked to the labels with the corresponding text fields.

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