- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaFX Label setLabelFor() method example
In JavaFX, you can create a label by instantiating the javafx.scene.control.Label class. This class provides a method named labelFor(). Using this method, you can set the current label as a label for another control node.
This method comes handy while setting, mnemonics, and accelerator parsing.
Example
In the following JavaFX example, we have created a label, a text field, and a button. Using the labelFor() method we have associated the label (Text) with a text field, enabling the mnemonic parsing (T). Therefore, on the output window, if you press Alt + t, the text field will be focused.
import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.HBox; import javafx.scene.paint.Color; import javafx.stage.Stage; public class LabelFor_Example extends Application { public void start(Stage stage) { //Creating nodes TextField textField = new TextField(); Button button = new Button("Click Me"); //creating labels Label label1 = new Label("_Text"); label1.setMnemonicParsing(true); label1.setLabelFor(textField); //Adding labels for nodes HBox box1 = new HBox(5); box1.setPadding(new Insets(25, 5 , 5, 50)); box1.getChildren().addAll(label1, textField, button); //Setting the stage Scene scene = new Scene(box1, 595, 150, Color.BEIGE); stage.setTitle("JavaFX Example"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
Output
- Related Articles
- How to create a label using JavaFX?
- How to set mnemonics in a label using JavaFX?
- How to add an image as label using JavaFX?
- JavaFX LineChart example with category axis
- How to wrap the text of a label in JavaFX?
- JavaFX example to set behavior to a button
- JavaFX line chart example with multiple series (lines)
- JavaFX example to set action listeners to a CheckBox
- JavaFX example to create area chart with multiple series
- JavaFX example to create area chart with negative values
- JavaFX example to set slider to the progress bar
- What is CheckBoxTreeItem in JavaFX explain with an example?
- JavaFX example to apply multiple transformations on a node
- JavaFX example to set action to the "exit" MenuItem
- JavaFX example to set action (behavior) to the menu item

Advertisements