
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Set Mnemonics in a Label Using JavaFX
You can display a text element/image on the User Interface using the Label component. It is a not editable text control, mostly used to specify the purpose of other nodes in the application.
In JavaFX, you can create a label by instantiating the javafx.scene.control.Label class.
Setting mnemonic
A mnemonic is a number or character, in the menu title of User interface component (button, text field, etc.) typically with an underscore. If you press this character along with the Alt key the respective menu item will be focused.
To create a mnemonic −
Create any node by instantiating its respective class.
Create a label to associate the node, with an underscore character (“_”) before the desired mnemonic character.
By default, the Mnemonic Parsing value for a label will be false, set this value to true using the setMnemonicParsing() method.
Set/Associate the label to the desired node.
Add the label and field to the scene.
Example
import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.PasswordField; import javafx.scene.control.TextField; import javafx.scene.layout.HBox; import javafx.scene.paint.Color; import javafx.stage.Stage; public class LabelForExample extends Application { public void start(Stage stage) { //Creating nodes TextField textField = new TextField(); PasswordField pwdField = new PasswordField(); //creating labels Label label1 = new Label("_Email"); label1.setMnemonicParsing(true); label1.setLabelFor(textField); Label label2 = new Label("_Password"); label2.setMnemonicParsing(true); label2.setLabelFor(pwdField); //Adding labels for nodes HBox box1 = new HBox(5); box1.setPadding(new Insets(25, 5 , 5, 50)); box1.getChildren().addAll(label1, textField, label2, pwdField); //Setting the stage Scene scene = new Scene(box1, 595, 150, Color.BEIGE); stage.setTitle("Check Box Example"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
Output
IF you press Alt+e in the keyboard, the focus will be shifted to the 1st text field and, if you press Alt+p in the keyboard the focus will be shifted to the 2nd text field