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:
- Create a class by extending the
Application
class and implementing thestart()
method. - Create the text by instantiating the class
Text
. Then setx
andy
position usingsetX()
andsetY()
methods. - Create a
group
class. - Create a scene object, instantiate the
scene
class, and pass thegroup
object to thescene
. - Add a title to the stage by the
setTitle
method, and add the scene to the stage using thesetScene()
method. - 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:
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:
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 FacebookRelated Article - Java JavaFX
- InvocationTargetException in JavaFX
- Play a Video Using JavaFX
- Solution to the JavaFX FXML Load Exception
- Create JavaFX Message Box
- JavaFX Media Player
- Create Square in JavaFX