How to create a JavaFX Label

If it’s a first time you are creating a JavaFX Application, you need to install Java SE Development Kit and NetBeans IDE on your computer.  Then Open NetBeans IDE click on File menu and select New Project, a dialog box appears, choose JavaFX from the list and click Next Button. Type “JavaFXControls” in the text field and click Finish Button.

JavaFX UI controls
Your project structure should look somewhat like this.

Create a simple label with a text :

Let’s create a label with JavaFX. Below is a java class called “JavaFxControls”.

Label fxLabel1 = new Label(); //creating a new label called fxLabel1
fxLabel1.setText("New JavaFX Label"); //Setting the text of fxLabel1

package javafxcontrols;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class JavaFxControls extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        
        Label fxLabel1 = new Label(); //creating a new label called fxLabel1
        fxLabel1.setText("New JavaFX Label"); //Setting the text of fxLabel1
       
//      Label fxLabel1 = new Label("New JavaFX Label");
//      you can also create a label with text in a single line using constructor arguments as above line

        StackPane root = new StackPane();
        root.getChildren().add(fxLabel1);
        Scene scene = new Scene(root, 500, 500);
        
        primaryStage.setTitle("Creating a Label with JavaFX");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
    
}

Replace the auto generated code in JavaFxControls.java file as above code. We should import the javafx.scene.control.Label library to use a Label.

JavaFX UI controls

If it is missing a bulb icon appears in the code, you can click on it and select the library name, NetBeans IDE automatically adds import javafx.scene.control.Label; line at the top. Now click on Run menu and select your project name.

JavaFX UI controls

Your application will show a label named New JavaFX Label.

Create a label with a graphics and fill the text with a color :

JavaFX UI controls

Right click on this image and choose “Save Image As” option and download to you computer. Rename it as username-label.png and move it to the src folder of your NetBeans Project.

JavaFX UI controls

 

package javafxcontrols;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class JavaFxControls extends Application {
    
    @Override
    public void start(Stage primaryStage) {
                
        Image image = new Image(getClass().getResourceAsStream("/username-label.png"));

        Label fxLabelWithImage = new Label();
        fxLabelWithImage.setText("Username");
        fxLabelWithImage.setGraphic(new ImageView(image));
        
//      Label fxLabelWithImage = new Label("Username",new ImageView(image));
//      you can also create a label with text and image in a single line using constructor arguments as above

        fxLabelWithImage.setTextFill(Color.web("rgb(255,0,0)"));
       
        StackPane root = new StackPane();
        root.getChildren().add(fxLabelWithImage);
        Scene scene = new Scene(root, 500, 500);
        
        primaryStage.setTitle("Creating a Label with JavaFX");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
    
}

Image image = new Image(getClass().getResourceAsStream("/username-label.png"));
we defined a image resource file for the image called username-label.png inside the src folder. We pass the location of a image file using the method getResourceAsStream(). If you wish to use your own image file, replace the method argument with location of your image file. The method .setText(“Username”) sets the text of label as Username , .setGraphic(new ImageView(image)) sets the graphic image file for the label and setTextFill(Color.web(“rgb(255,0,0)”)) fills the text with red color. Now Run your Project, you will see the JavaFX application like below

JavaFX

Apply some font style and wrap the text :

package javafxcontrols;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class JavaFxControls extends Application {
    
    @Override
    public void start(Stage primaryStage) {

        Label fxLabel = new Label();
        fxLabel.setText("A Label with a very long text which is longer than the scene container");
        fxLabel.setTextFill(Color.web("rgb(255,0,0)"));
        fxLabel.setFont(new Font("Arial", 32));
        //fxLabelWithImage.setFont(Font.font("Arial", 32));
        //Alternatively you can use font's .font() method to set font type and font size
        //fxLabel.setWrapText(true);
        

        StackPane root = new StackPane();
        root.getChildren().add(fxLabel);
        Scene scene = new Scene(root, 500, 500);
        
        primaryStage.setTitle("Creating a Label with JavaFX");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
    
}

Replace your code with above. Here we make use new method of JavaFX label called .setFont(new Font(“Arial”, 32))  which sets the font type of label as Arial and font size as 32. Now Run you project.

JavaFx label

You will see something like this, do you see something wrong here. The label text is only partially visible, this is due to the length of the label text exceeded the stackpane container. So, to fit the text in fixed width container we can wrap it using setWrapText() method.

fxLabel.setWrapText(true);

Apply some effects to label :

package javafxcontrols;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class JavaFxControls extends Application {
    
    @Override
    public void start(Stage primaryStage) {

        Label fxLabel = new Label();
        fxLabel.setText("Applying Effects");
        fxLabel.setTextFill(Color.web("rgb(255,0,0)"));
        fxLabel.setFont(new Font("Arial", 32));
       
        fxLabel.setOnMouseEntered((MouseEvent e) -> {
            fxLabel.setScaleX(1.8);
            fxLabel.setScaleY(1.8);
        });

        fxLabel.setOnMouseExited((MouseEvent e) -> {
            fxLabel.setScaleX(1);
            fxLabel.setScaleY(1);
        });
        
        StackPane root = new StackPane();
        root.getChildren().add(fxLabel);
        Scene scene = new Scene(root, 500, 500);
        
        primaryStage.setTitle("Creating a Label with JavaFX");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
    
}

package javafxcontrols;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class JavaFxControls extends Application {
    
    @Override
    public void start(Stage primaryStage) {

        Label fxLabel = new Label();
        fxLabel.setText("Applying Effects");
        fxLabel.setTextFill(Color.web("rgb(255,0,0)"));
        fxLabel.setFont(new Font("Arial", 32));
       
        fxLabel.setOnMouseEntered((MouseEvent e) -> {
            fxLabel.setRotate(312);
            fxLabel.setTranslateX(30);
            fxLabel.setScaleX(2.5);
            fxLabel.setScaleY(2.5);
        });

        fxLabel.setOnMouseExited((MouseEvent e) -> {
            fxLabel.setRotate(0);
            fxLabel.setTranslateX(0);
            fxLabel.setScaleX(1);
            fxLabel.setScaleY(1);
        });
        
        StackPane root = new StackPane();
        root.getChildren().add(fxLabel);
        Scene scene = new Scene(root, 500, 500);
        
        primaryStage.setTitle("Creating a Label with JavaFX");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
    
}