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.

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.

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.

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.

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:

Explanation
- 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, - 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. - 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package javafxproj; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.GridPane; import javafx.stage.Stage; public class JavaFxProj extends Application { @Override public void start(Stage primaryStage) { //Sample 01: Create two Label Controls Label lblPersonName = new Label("_Person Name:"); Label lblAge = new Label("_Age:"); //Sample 02: Create Two Text Fields TextField tfPersonName = new TextField(); TextField tfAge = new TextField(); //Sample 03: Associate Label with Text Fields lblPersonName.setLabelFor(tfPersonName); lblAge.setLabelFor(tfAge); //Sample 04: Enable Mnemonic Parsing lblPersonName.setMnemonicParsing(true); lblAge.setMnemonicParsing(true); //Sample 05: Add Controls to the Grid Pane GridPane gPane = new GridPane(); gPane.addRow(0, lblPersonName, tfPersonName); gPane.addRow(1, lblAge, tfAge); //Sample 06: Show the Stage Scene scene = new Scene(gPane); primaryStage.setTitle("Label Control Mnemonic Example"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } |
Categories: JavaFx
Tags: JavaFx, JavaFx Label, LabelFor, Mnemonic, Mnemonic Parsing