- JavaFX Tutorial
- JavaFX - Home
- JavaFX - Overview
- JavaFX Installation and Architecture
- JavaFX - Installation Using Netbeans
- JavaFX - Installation Using Eclipse
- JavaFX - Installation using Visual Studio Code
- JavaFX - Architecture
- JavaFX - Application
- JavaFX 2D Shapes
- JavaFX - 2D Shapes
- JavaFX - Drawing a Line
- JavaFX - Drawing a Rectangle
- JavaFX - Drawing a Rounded Rectangle
- JavaFX - Drawing a Circle
- JavaFX - Drawing an Ellipse
- JavaFX - Drawing a Polygon
- JavaFX - Drawing a Polyline
- JavaFX - Drawing a Cubic Curve
- JavaFX - Drawing a Quad Curve
- JavaFX - Drawing an Arc
- JavaFX - Drawing an SVGPath
- JavaFX Properties of 2D Objects
- JavaFX - Stroke Type Property
- JavaFX - Stroke Width Property
- JavaFX - Stroke Fill Property
- JavaFX - Stroke Property
- JavaFX - Stroke Line Join Property
- JavaFX - Stroke Miter Limit Property
- JavaFX - Stroke Line Cap Property
- JavaFX - Smooth Property
- Operations on 2D Objects
- JavaFX - 2D Shapes Operations
- JavaFX - Union Operation
- JavaFX - Intersection Operation
- JavaFX - Subtraction Operation
- JavaFX Color and Texture
- JavaFX - Colors
- JavaFX Text
- JavaFX - Text
- JavaFX Effects
- JavaFX - Effects
- JavaFX Transformations
- JavaFX - Transformations
- JavaFX Animations
- JavaFX - Animations
- JavaFX Images
- JavaFX - Images
- JavaFX 3D Shapes
- JavaFX - 3D Shapes
- JavaFX - Creating a Box
- JavaFX - Creating a Cylinder
- JavaFX - Creating a Sphere
- Properties of 3D Objects
- JavaFX - Cull Face Property
- JavaFX - Drawing Modes Property
- JavaFX - Material Property
- JavaFX Event Handling
- JavaFX - Event Handling
- JavaFX - Using Convenience Methods
- JavaFX - Event Filters
- JavaFX - Event Handlers
- JavaFX UI Controls
- JavaFX - UI Controls
- JavaFX - ListView
- JavaFX - Accordion
- JavaFX - ButtonBar
- JavaFX - ChoiceBox
- JavaFX - HTMLEditor
- JavaFX - MenuBar
- JavaFX - Pagination
- JavaFX - ProgressIndicator
- JavaFX - ScrollPane
- JavaFX - Separator
- JavaFX - Slider
- JavaFX - Spinner
- JavaFX - SplitPane
- JavaFX - TableView
- JavaFX - TabPane
- JavaFX - ToolBar
- JavaFX - TreeView
- JavaFX - Label
- JavaFX - CheckBox
- JavaFX - RadioButton
- JavaFX - TextField
- JavaFX - PasswordField
- JavaFX - FileChooser
- JavaFX - Hyperlink
- JavaFX - Tooltip
- JavaFX - Alert
- JavaFX - DatePicker
- JavaFX - TextArea
- JavaFX Charts
- JavaFX - Charts
- JavaFX - Creating Pie Chart
- JavaFX - Creating Line Chart
- JavaFX - Creating Area Chart
- JavaFX - Creating Bar Chart
- JavaFX - Creating Bubble Chart
- JavaFX - Creating Scatter Chart
- JavaFX - Creating Stacked Area Chart
- JavaFX - Creating Stacked Bar Chart
- JavaFX Layout Panes
- JavaFX - Layout Panes
- JavaFX - HBox Layout
- JavaFX - VBox Layout
- JavaFX - BorderPane Layout
- JavaFX - StackPane Layout
- JavaFX - TextFlow Layout
- JavaFX - AnchorPane Layout
- JavaFX - TilePane Layout
- JavaFX - GridPane Layout
- JavaFX - FlowPane Layout
- JavaFX CSS
- JavaFX - CSS
- Media with JavaFX
- JavaFX - Playing Video
- JavaFX Useful Resources
- JavaFX - Quick Guide
- JavaFX - Useful Resources
- JavaFX - Discussion
JavaFX - Label
A Label is a piece of text that describe or informs users about the functionality of other elements in the application. It helps in reducing confusion and provides clarity which leads to a better user experience. Always remember, it is a not an editable text control. In the figure below, we can see a button in red box and there are some text describing its purpose −
Label in JavaFX
In JavaFX, the label is represented by a class named Label which belongs to the javafx.scene.control package. To create a label in JavaFX application, we can use any of the below constructor −
Label() − It is the default constructor that constructs an empty label.
Label(String str) − It constructs a label with the predefined text.
Label(String str, Node graph) − It constructs a new label with the specified text and graph.
Steps to create a Label in JavaFX
To create a Label in JavaFX, follow the steps given below −
Step 1: Instantiate the Label class
As discussed earlier, we need to instantiate the Label class to create a label text. We can use either its default constructor or parameterized constructor. If we use the default one, the label text is added by using the setText() method.
// Instanting the Label class Label label = new Label("Sample label");
Step 2: Set the required properties of Label
Just like a text node we can set the desired properties like font and font color to the label node in JavaFX using the setFont() method and setFill() method respectively.
// 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);
Step 4: Launching Application
Once the Label is created and its properties are set, define a group object to hold the label. Next, create a Scene object by passing the group obejct and the dimensions of the Scene to its constructor. Then, set the stage and launch the application to display the result.
Example
In the following example, we are going to create a Label in JavaFX application. Save this code in a file with the name JavafxLabel.java.
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 JavafxLabel 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, 400, 300, Color.BEIGE); stage.setTitle("Label Example"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
To compile and execute the saved Java file from the command prompt, use the following commands −
javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxLabel.java java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxLabel
Output
When we execute the above code, it will generate a Label text as shown in the following output.