JavaFX Display Text

Sheeraz Gul Jul 07, 2022
JavaFX Display Text

The text can be created and displayed using the JavaFX.scene.text.Text class. This tutorial demonstrates how to display single and multiline text in JavaFX.

JavaFX Display Text

The JavaFX.scene,text.Text is used to create and display text in JavaFX. A text node can be created by instantiating the Text class and displayed in the scene.

Syntax:

Text text = new Text(text);

Where the text as the parameter is the text value. To set the value of the x and y position of the text, we use the following methods:

text.setX(30);
text.setY(30);

The above methods will set the position of text according to the x and y positions given in the methods. Follow the steps below to create and display text in JavaFX:

  1. Create a class by extending the Application class and implementing the start() method.
  2. Create the text by instantiating the class Text. Then set x and y position using setX() and setY() methods.
  3. Create a group class.
  4. Create a scene object, instantiate the scene class, and pass the group object to the scene.
  5. Add a title to the stage by the setTitle method, and add the scene to the stage using the setScene() method.
  6. Display the stage using the show() method and launch the application.

Let’s implement an example based on the steps above.

Example Code:

package delftstack;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.text.Text;

public class JavaFX_Display_Text extends Application {
    @Override
    public void start(Stage Demo_Stage) {
        //Create a Text object
        Text Demo_Text = new Text();

        //Set the text to be added.
        Demo_Text.setText("Hello, This is delftstack.com");

        //set the position of the text
        Demo_Text.setX(80);
        Demo_Text.setY(80);

        //Create a Group object
        Group Group_Root = new Group(Demo_Text);

        //Create a scene object
        Scene Demo_Scene = new Scene(Group_Root, 600, 300);

        //Set title to the Stage
        Demo_Stage.setTitle("Text Display");

        //Add scene to the stage
        Demo_Stage.setScene(Demo_Scene);

        //Display the contents of the stage
        Demo_Stage.show();
    }
    public static void main(String args[]){
        launch(args);
    }
}

The code above will create and display the Text on the scene.

Output:

JavaFX Display Text

We can use a Label instead of the Text to display multiline text. Create a Label and pass the Text to it.

We must wrap the Text in a Label to show it as a multiline text.

Example Code:

package delftstack;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class JavaFX_Display_Text extends Application {
    @Override
    public void start(Stage Demo_Stage) {
        String Content = "DelftStack is a resource for everyone interested in programming, "
                + "embedded software, and electronics. It covers the programming languages "
                + "like Python, C/C++, C#, and so on in this website's first development stage. "
                + "Open-source hardware also falls in the website's scope, like Arduino, "
                + "Raspberry Pi, and BeagleBone. DelftStack aims to provide tutorials, "
                + "how-to's, and cheat sheets to different levels of developers and hobbyists..";
        //Create a Label
        Label Demo_Text = new Label(Content);
        //wrap the label
        Demo_Text.setWrapText(true);
        //Set the maximum width of the label
        Demo_Text.setMaxWidth(300);
        //Set the position of the label
        Demo_Text.setTranslateX(30);
        Demo_Text.setTranslateY(30);
        Group Text_Root = new Group();
        Text_Root.getChildren().add(Demo_Text);
        //Set the stage
        Scene Text_Scene = new Scene(Text_Root, 595, 150, Color.BEIGE);
        Demo_Stage.setTitle("Display Multiline Text");
        Demo_Stage.setScene(Text_Scene);
        Demo_Stage.show();
    }
    public static void main(String args[]){
        launch(args);
    }
}

The code above will show the text wrapped in a label as multiline.

Output:

JavaFX Display Multiline Text

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java JavaFX