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 −

JavaFX Label

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.

Javafx Label Output
Advertisements