Table of contents
1.
Introduction
2.
JavaFX Label
2.1.
Label Constructors
2.2.
Label Methods
3.
JavaFX Label Example
3.1.
Example1
3.1.1.
Code
3.1.2.
Output
3.1.3.
Explanation
3.2.
Example2
3.2.1.
Code
3.2.2.
Output
3.2.3.
Explanation
4.
Frequently Asked Questions
4.1.
How can we set change the text for a label in JavaFX?
4.2.
How can we set the font for a label in JavaFX?
4.3.
What is a Stage in JavaFX?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

JavaFX Label

Author Aniket Majhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hello and Welcome, readers! We hope that you are doing well.

JavaFX is a Java library consisting of graphics and media packages for creating, designing, debugging, testing and deploying rich client applications that can operate across multiple platforms.

If you want to learn more about JavaFX, follow the article, JavaFX.

This article will discuss the JavaFX Label with proper explanations and implementations. We will show you different examples for a better understanding of the topic. If you want to grasp the topic entirely, follow the article until the end.
 

So, without further ado, let's get started.

JavaFX Label

In JavaFX, a Label is a not editable text control used to display texts or images inside a JavaFX user interface. In order to be visible, the label control must be added to the scene graph. Though it can be used to display small texts or images, it can't be focused.

The label in JavaFX is under the package of javafx.scene.control, represented by javafx.scene.control.Label class.

Label Constructors

The JavaFX Lable has three constructors:

  1. Label(): This will create an empty label.
  2. Label(String s): This will create a label with the text.
  3. Label(String s, Node n): This will create a label with the given text and the image.

Label Methods

The commonly used JavaFX Label methods are as follows:

  • createDefaultSkin(): It creates a new instance of the default skin for the specified control.
  • labelForProperty(): A Lable can behave like a label for another node or control. 
  • getLabelFor(): It returns the value of the labelFor property.
  • setLabelFor(Node n): It sets the value of the labelFor property.

JavaFX Label Example

In this section, we will be dealing with some examples related to labels in JavaFX.

Example1

In this example, we will show you how to create a simple text label using JavaFX.

Code

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.*;
import javafx.scene.text.TextAlignment;

public class App extends Application {

    // launch application
    public void start(Stage s) {

        // set the title of the stage created
        s.setTitle("Simple Text Label Example");
        Label lb1 = new Label("This is a Coding Ninjas.");
        // create a scene with (350 x 100) resolution
        Scene sc = new Scene(lb1, 350, 100);

        // finally host the scene inside the stage
        s.setScene(sc);

        // display the result
        s.show();

    }

    public static void main(String args[]) {
        // launch the application
        launch(args);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

Explanation

Below is a simple explanation of the code:

  • We first set the stage title inside the start method using the method setTitle().
  • Then, We created a label using the Label(String s) constructor.
  • We created a scene with the Label by setting the dimension as 350 x 100.
  • Then, we added the scene to the stage.
  • Finally, the show() method displays the result.

Example2

In this example, we will show you how to create a simple Image label with a test using JavaFX.

Code

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.*;
import javafx.scene.image.*;

public class App extends Application {

    // launch application
    public void start(Stage s) {

        // set the title of the stage created
        s.setTitle("Simple Text Label Example");
        try {
            // Taking the image file from the appropriate location
            FileInputStream inp = new FileInputStream("C:/Users/Aniket Majhi/Desktop/images.jpg");
           
            // Constructing image from the input stream
            Image img = new Image(inp);

            // creating Node for the image
            ImageView imgvw = new ImageView(img);

            // Image Label with text
            Label lb = new Label("This is Coding Ninjas", imgvw);

            Scene sc = new Scene(lb, 400, 300);

            // finally host the scene inside the stage
            s.setScene(sc);

            // display the result
            s.show();
        } catch (FileNotFoundException e) {
            System.out.println("File Not Found");
        }

    }

    public static void main(String args[]) {
        // launch the application
        launch(args);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

Explanation

Below is the description of the above code:

  • First, we took the image file from the specified location using FileInputStream().
  • Then we constructed the image using Image() with the content of the input stream.
  • Then, we created a Node for the Image using ImageView() class.
  • We created an Image label with text using Label(String s, Node n).
  • Then, We created a scene with the Label by setting the dimension as 400 x 300.
  • Then, we added the scene to the stage.
  • Finally, the show() method displays the result.

Frequently Asked Questions

How can we set change the text for a label in JavaFX?

We can change the text for a label using the setText() method. You can do it while the application is running.

How can we set the font for a label in JavaFX?

We can change the font for a label by calling the setFont() method in JavaFX. This method makes you change the size as well as the style of the text. 

What is a Stage in JavaFX?

The JavaFX Stage is the top-level JavaFX container consisting of all the objects of a JavaFX application. The platform itself creates the primary stage. The Stage is under the javafx.stage and is represented by the javafx.stage.Stage class. To display the content of the stage, you need to call the show() method.

Conclusion

In this article, we have extensively discussed the JavaFX Label.

We started with the basic introduction. Then we discussed JavaFX Label, JavaFX Label Constructors, and JavaFX Label Methods. Finally, we showed some examples.

We hope that this blog gives you some ideas regarding JavaFX Label. If you would like to learn more, follow our articles on  JavaFXExplore our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations and much more.!

Do upvote this blog to help other ninjas grow.

Happy Reading!

Live masterclass