- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to 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
- Related Articles
- How to set mnemonics in a label using JavaFX?
- How to add an image as label using JavaFX?
- How to create a circle using JavaFX?
- How to create a Rectangle using JavaFX?
- How to create a Line using JavaFX?
- How to create a Polygon using JavaFX?
- How to create a Polyline using JavaFX?
- How to create a CubicCurve using JavaFX?
- How to create a QuadCurve using JavaFX?
- How to create a separator using JavaFX?
- How to create a Menu using JavaFX?
- How to create a ScrollBar using JavaFX?
- How to create a ListView using JavaFX?
- How to create a TreeView using JavaFX?
- How to create a pagination using JavaFX?

Advertisements