
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
Create 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.
Just like a text node you can set the desired font to the text node in JavaFX using the setFont() method and, you can add color to it using the setFill() method.
To create a label −
Instantiate the Label class.
Set the required properties to it.
Add the label to the scene.
Example
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.scene.text.FontPosture; import javafx.scene.text.FontWeight; import javafx.stage.Stage; public class LabelExample extends Application { public void start(Stage stage) { //Creating a Label Label label = new Label("Sample label"); //Setting font to the label Font font = Font.font("Brush Script MT", FontWeight.BOLD, FontPosture.REGULAR, 25); label.setFont(font); //Filling color to the label label.setTextFill(Color.BROWN); //Setting the position label.setTranslateX(150); label.setTranslateY(25); Group root = new Group(); root.getChildren().add(label); //Setting the stage Scene scene = new Scene(root, 595, 150, Color.BEIGE); stage.setTitle("Label Example"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
Output
Advertisements