Updated July 7, 2023
Introduction to JavaFX Text
Sometimes, in JavaFX, the text has to be provided on the user interface of the application. For this, the library of JavaFX offers a class javafx.scene.text.Text. Therefore, this class has to be instantiated if you want to create a text. Properties of this can be altered using different methods. Syntax, properties, and program to implement JavaFX Text will be discussed in the below sections.
Syntax:
Syntax to initialize are:
Text t = new Text();
t.setText("Hello !! This is my syntax");
Constructors of JavaFX Text
Below are the three constructors are available. They are:
- Text(): An empty text instance will be created.
- Text(double x, double y, Stringtext): A text instance will be created on the coordinates x and y that contain the given string.
- Text(Stringtext): A text instance will be created with the given string.
Properties of JavaFX Text
Below are the several properties, they are:
- setBoundsType(TextBoundsType v): Property which is of an object type that helps in determining how the bounds of text are calculated.
- setLineSpacing(double s): Between the Lines, a vertical space s will be set.
- setFont(Font value): Text font will be set using this method.
- setText(String value): Text string to be displayed will be set using this method.
- setX(double value): This method will set the x coordinate of the text.
- setY(double value): This method will set the y coordinate of the text.
- setStrikeThrough(boolean value): Text that is displayed will be striken with a line using this method.
- setTextOrigin(VPos value): Sets the text coordinate system origin in the local coordinate system.
- setWrappingWidth(double value): Text width limit from where the text has to be wrapped will be set using this method.
- setFontSmoothingType(FontSmoothingType value): Smoothing type mentioned will be set for the font with the help of this method.
- setTextAlignment(TextAlignment value): Sets the Horizontal Text alignment.
- setUnderLine(boolean value): Text that is displayed will be underlined using this method.
- getFont(): Font property’s value will be returned.
- getText(): Text property’s value will be returned.
- getLineSpacing(): LineSpacing property’s value will be returned.
- getWrappingWidth(): WrappingWidth property’s value will be returned.
- getTextOrigin(): Text Origin property’s value will be returned.
- getTextAlignment(): Text Alignment property’s value will be returned.
- getFontSmoothingType(): fontSmoothingType property’s value will be returned.
- isStrikethrough(): strikethrough property’s value will be returned.
- isUnderline(): underline property’s value will be returned.
- toString(): A string representation of the given Text object will be returned.
Program to Implement JavaFX Text
Now, let us see some of the programming examples for JavaFX Texts.
Example #1 – Program to display a simple Text.
Code:
//Java program to display a simple text
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.text.Text;
//class that extends Application base class
public class JavaFXTextExample extends Application {
//application starts at this point
@Override
public void start(Stage s) {
//create a text
Text t = new Text();
//Set the text to be displayed
t.setText("Hey ... This is the sample text !!!");
//set the X and Y coordinate
t.setX(50);
t.setY(50);
//Create a Group object
Group r = new Group(t);
//Create a scene
Scene sc = new Scene(r, 400, 300);
//Set title to the Stage
s.setTitle("Sample for displaying JavaFX Text");
//Add scene to the stage
s.setScene(sc);
//Display the results of the stage
s.show();
}
//main method
public static void main(String args[]){
launch(args);
}
}
Output:
A text will be displayed as the output on executing the code.
Example #2 – Java Program to display customized font.
Code:
//Java program to display customized font
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
//class that extends Application base class
public class JavaFXTextExample extends Application {
//application starts at this point
@Override
public void start(Stage s) {
//create a text
Text t = new Text();
//Set the text to be displayed
t.setText("Hey ... This is the sample text !!!");
//set the X and Y coordinate
t.setX(50);
t.setY(50);
t.setFont(Font.font("Times New Roman",FontWeight.BOLD,FontPosture.REGULAR,45));
//Create a Group object ; vbox can also be created in this step
Group r = new Group(t);
//Create a scene
Scene sc = new Scene(r, 700, 300);
//Set title to the Stage
s.setTitle("Sample for displaying JavaFX Text");
//Add scene to the stage
s.setScene(sc);
//Display the results of the stage
s.show();
}
//main method
public static void main(String args[]){
launch(args);
}
}
Output:
Here, a text is displayed with the font, font size, etc. set by the user.
Example #3 – Program to display texts with different styles.
Code:
//Java program to display texts with several styles
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
//child class that inherits Application class
public class JavaFXTextExample extends Application
{
public static void main(String[] args)
{
Application.launch(args);
}
//application starts at this point
@Override
public void start(Stage s)
{
// Create the text 1
Text tx1 = new Text("This is a text which is stroked with red and white color");
//set text color
tx1.setStroke(Color.RED);
tx1.setFill(Color.WHITE);
//set text font size
tx1.setFont(new Font(20));
// Create the text 2
Text tx2 = new Text("This is a text with an Underline");
//set underline
tx2.setUnderline(true);
// Create the text 3
Text tx3 = new Text("This is a text which is striked with a line");
tx3.setStrikethrough(true);
// Create VBox
VBox vb = new VBox();
// Add the Text to the VBox
vb.getChildren().addAll(tx1, tx2, tx3);
// Set Spacing as 30 px
vb.setSpacing(20);
// Set the Styles of the VBox
vb.setStyle("-fx-padding: 10;" +
"-fx-border-style: solid inside;" +
"-fx-border-width: 2;" +
"-fx-border-insets: 5;" +
"-fx-border-radius: 5;" +
"-fx-border-color: red;");
// Create Scene
Scene sc = new Scene(vb);
// Add scene to the Stage s
s.setScene(sc);
// Set the title for the Stage
s.setTitle("Text with decorations");
// Display the Stage
s.show();
}
}
Output:
A dialog box appears with 3 texts- one which is stroked and filled with color, other which has an underline and the last one which is struck.
Conclusion
Texts in JavaFX are used to create a text especially in User interfaces of an application. it is instantiated from the class javafx.scene.text.Text. Properties, constructors, syntax, and implementation of JavaFX Text is clearly discussed in this document.
Recommended Articles
This is a guide to JavaFX Text. Here we discuss the programming examples for JavaFX Texts, with properties, syntax, and constructors. You can also go through our other related articles to learn more –