Table of contents
1.
Introduction
2.
JavaFX 
3.
JavaFX Text 
4.
Properties of JavaFX Text 
5.
Creating Text Node
5.1.
Example
5.2.
Output
6.
Applying for Font and position 
6.1.
Syntax
6.2.
Example
6.3.
Output
7.
Applying Stroke and Colour
7.1.
Example
7.2.
Output
8.
Applying Decoration
8.1.
Example
8.2.
Output
9.
Frequently Asked Question
9.1.
What is JavaFX?
9.2.
What is the package?
9.3.
Are audio and video codecs supported by JavaFX?
9.4.
What is the @override statement?
9.5.
What is text in JavaFX?
10.
Conclusion
Last Updated: Mar 27, 2024
Medium

JavaFX Text

Author Muskan Sharma
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

While making the interface of an application many times, we need to provide information through text. But usually you have to add many features to add introductory text information. But, the topic we will learn in this blog will decrease code complexity because of JavaFX

JavaFX provides a Class named  javafx.scene.text.Text. This class offers several ways to change the text's various attributes. So let's learn more about JavaFX Text. But before that, let's have a look at JavaFX.

 

JavaFx Text

JavaFX 

JavaFX is a GUI toolkit and Java framework created to support the creation of desktop, online, and Rich Internet Application (RIA) software. The main benefit of utilizing JavaFX is that programmes created with it may run on various platforms, including desktops, the web, mobile phones, TVs, tablets, and operating systems like Windows, Linux, iOS, and Android. Due to its ability to run across various platforms and operating systems, the JavaFX library possesses this feature.

JavaFX Text 

On the application's interface, we must display text-based information. A class called javafx. scene. the JavaFX library offers text. This class provides numerous ways to change different textual attributes.

Properties of JavaFX Text 

Properties

Description

Setter Methods

boundstype It's an object-type property. It establishes the methodology used to calculate the text's boundaries. setBoundsType(TextBoundsType value)
font It is the Font of the text. setFont(Font value)
linespacing Lines are separated vertically by pixels. This property is of the double type. setLineSpacing(double spacing)
strikethrough This property is of the boolean type. Setting this parameter to true will allow us to insert a line through the text. setStrikeThrough(boolean value)
textalignment Text alignment in the horizontal direction setTextAlignment(TextAlignment value)
textorigin Local coordinate system is where the text coordinate system originated. setTextOrigin(VPos value)
text It is a property of the string type. It specifies the text string that will be shown. setText(String value)
underline It is a property of the boolean type. Setting this parameter to true will cause the text to be underlined. setUnderLine(boolean value)
x X coordinate of the text setX(double value)
y Y coordinate of the text setY(double value)

 

Now let's look at how to create a node.

Creating Text Node

Text text = new Text();

The text that will be created is represented by a string-type property called text in the class Text.

You must use the setText() function to assign a value to this property after creating an instance of the Text class, as illustrated below.

String text = "Hello!! Welcome Ninjas"
Text.setText(text);

Now let’s see the example.

Example

The Text class is demonstrated in the example that follows. Since the text positions are not defined in this instance, the text will be displayed in the center of the screen.

package application;  
import javafx.application.Application;  
import javafx.scene.Scene;  
import javafx.scene.layout.StackPane;  
import javafx.scene.text.Text;  
import javafx.stage.Stage;  
public class TextExample extends Application{  
@Override  
public void start(Stage primaryStage) throws Exception {  
    // TODO Auto-generated method stub  
    Text text = new Text();  
    text.setText("Hello !! Welcome Ninjas");  
    StackPane root = new StackPane();  
    Scene scene = new Scene(root,300,400);  
    root.getChildren().add(text);  
    primaryStage.setScene(scene);  
    primaryStage.setTitle("Text CodingNinjas");  
    primaryStage.show();  
}  
public static void main(String[] args) {  
    launch(args);  
      
}  
}

Output

Output

Applying for Font and position 

We can use JavaFX to apply different fonts to the text nodes. Use the setter method setFont to set the Text class's font property (). This method will take a Font class object. The package javafx. scene. the text contains the class Font. There is a static method called Font (). This produces an object of the Font type, which the Text class's setFont() method will accept as a parameter. The following parameters can be passed to the Font. font() function.

  1. Family: It serves as a representation of the font family. It should already be in the system and be of the string type.
  2. Weight: The Font's weight is controlled by this Font class parameter. The font weight can be set to one of nine settings. They are FontWeight values. BLACK, BOLD, EXTRA_BOLD, EXTRA_LIGHT, LIGHT, MEDIUM, NORMAL, SEMI_BOLD, THIN.
  3. Posture: This Font class property describes the Font's posture. FontPosture.ITALIC or FontPosture.REGULAR are also acceptable choices.
  4. Size: This property has a double type. It is employed to control text size.

Syntax

<text_object>.setFont(Font.font(<String font_family>, <FontWeight>, <FontPosture>, <FontSize>)

Example

package application;  
import javafx.application.Application;  
import javafx.scene.Group;  
import javafx.scene.Scene;  
import javafx.scene.text.Font;  
import javafx.scene.text.FontPosture;  
import javafx.scene.text.FontWeight;  
import javafx.scene.text.Text;  
import javafx.stage.Stage;  
public class TextExample extends Application{  
@Override  
public void start(Stage primaryStage) throws Exception {  
    // TODO Auto-generated method stub  
    Text text = new Text();  
      
    text.setX(100);  
    text.setY(20);  
    text.setFont(Font.font("Abyssinica SIL",FontWeight.BOLD,FontPosture.REGULAR,20));  
    text.setText("Hello!! Welcome Ninjas");  
    Group root = new Group();  
    Scene scene = new Scene(root,500,200);  
    root.getChildren().add(text);  
    primaryStage.setScene(scene);  
    primaryStage.setTitle("Text CodingNinjas");  
    primaryStage.show();  
}  
publicstaticvoid main(String[] args) {  
    launch(args);  
      
}  
}

Output

Applying Stroke and Colour

Stroke refers to the padding at the text's edge. JavaFX enables us to give the text a stroke and different colors. javafx.scene.text. The setStroke() function of the Text class takes an object of the Paint class as an argument. The color that will be painted on the stroke should only be passed. We may also change the stroke's width by supplying a width value of double type into the setStrokeWidth() method. Javafx.scene.text can be used to change the Text's color. SetFill is another method that the Text class offers (). Only the color that will be used to fill in the text has to be passed.

Example

package application;  
import javafx.application.Application;  
import javafx.scene.Group;  
import javafx.scene.Scene;  
import javafx.scene.paint.Color;  
import javafx.scene.text.Font;  
import javafx.scene.text.FontPosture;  
import javafx.scene.text.FontWeight;  
import javafx.scene.text.Text;  
import javafx.stage.Stage;  
public class TextExample extends Application{  
@Override  
publicvoid start(Stage primaryStage) throws Exception {  
    // TODO Auto-generated method stub  
    Text text = new Text();  
      
    text.setX(100);  
    text.setY(20);  
    text.setFont(Font.font("Abyssinica SIL",FontWeight.BOLD,FontPosture.REGULAR,25));  
    text.setFill(Color.BLUE);// setting colour of the text to blue   
    text.setStroke(Color.BLACK); // setting the stroke for the text    
    text.setStrokeWidth(1); // setting stroke width to 2   
    text.setText("Hey!! Welcome Ninjas");   
    Group root = new Group();  
    Scene scene = new Scene(root,500,200);  
    root.getChildren().add(text);  
    primaryStage.setScene(scene);  
    primaryStage.setTitle("Text CodingNinjas");  
    primaryStage.show();  
}  
public static void main(String[] args) {  
    launch(args);  
      
}  
}

 

Output

Output

Applying Decoration

By adjusting the strikethrough and underlining attributes of javafx .scene.text, we may add decorations to the text. text level.

<TextObject>.setStrikeThrough(Boolean value) //pass true to put a line across the text  
<TextObject>.setUnderLine(Boolean value) //pass true to underline the text

Example

package application;  
import javafx.application.Application;  
import javafx.scene.Group;  
import javafx.scene.Scene;  
import javafx.scene.text.Font;  
import javafx.scene.text.Text;  
import javafx.stage.Stage;  
public class TextExample extends Application{  
@Override  
public void start(Stage primaryStage) throws Exception {  
    // TODO Auto-generated method stub  
    Text text = new Text();  
      
    text.setX(100);  
    text.setY(40);  
    text.setFont(Font.font("Liberation Serif",25));  
    text.setText("Hello !!");   
    text.setStrikethrough(true);  
    Text text1=new Text();  
    text1.setX(100);  
    text1.setY(140);  
    text1.setText("Welcome Ninjas!");  
    text1.setFont(Font.font("Liberation Serif",25));  
    text1.setUnderline(true);  
    Group root = new Group();  
    Scene scene = new Scene(root,500,200);  
    root.getChildren().addAll(text,text1);  
    primaryStage.setScene(scene);  
    primaryStage.setTitle("Text CodingNinjas");  
    primaryStage.show();  
}  
public static void main(String[] args) {  
    launch(args);  
      
}  
}  

Output

Output

Frequently Asked Question

What is JavaFX?

A collection of graphics and media packages called JavaFX gives programmers the tools they need to create sophisticated client apps that work consistently on various platforms.

What is the package?

A Java package is a collection of sub-packages, interfaces, and classes of a common type.

Are audio and video codecs supported by JavaFX?

Media playback is simple to incorporate into every JavaFX application because of the standard APIs offered by JavaFX.

What is the @override statement?

The @Override annotation notifies the compiler that an element declared in a superclass is intended to be overridden.

What is text in JavaFX?

 The Text class defines text nodes. 'n' denotes paragraph breaks, and text is wrapped around paragraph boundaries.

Conclusion

Finally, you've concluded this article. 

Congratulations!! You learned about JavaFX text in this blog. You studied the different changes we can make in the text with the help of JavaFX Text. After reading this, are you eager to read more articles on the subject of JavaFX text? Don't worry; Coding Ninjas will take care of everything. 

Check out the awesome content on the Coding Ninjas Website:

JavaFX Inline Styles

JavaFX Menu

Please see our Code studio guided routes to learn more about Java, Competitive Programming, JavaScript, System Design, and other topics. Also, enroll in our courses and use the accessible sample tests and problems. For placement preparations, have a look at the interview experiences and interview bundle.

Thank you

Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass