JavaFX Text Example

In this JavaFX example, we will show you how to create text in the JavaFX application.

We can use javafx.scene.text.Text class to create text-based information on the interface of our JavaFX application. This class provides various methods to alter various properties of the text. We just need to instantiate this class to implement text in our application.

JavaFX Text Example

In this example, we will create a text information using Text class. Here, we are not setting the positions for the text therefore the text will be displayed to the center of the screen.

package com.javafx.examples;

import javafx.application.Application;

import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class JavaFXTextExample extends Application {

    @Override
    public void start(Stage stage) {
        initUI(stage);
    }

    private void initUI(Stage stage) {

        // create Text class object
        Text text = new Text();

        // add text using setText() method
        text.setText("JavaFX Text Example Tutorial");
        StackPane root = new StackPane();
        Scene scene = new Scene(root, 400, 300);
        root.getChildren().add(text);
        stage.setScene(scene);
        stage.setTitle("JavaFX Text Example");
        stage.show();
    }

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

Output


Comments