JavaFX | TextFlow Class

TextFlow class is a part of JavaFX. TextFlow class is designed to lay out rich text. It can be used to layout several Text nodes in a single text flow. TextFlow class extends Pane class.
Constructors of the class:
- TextFlow(): Create a new textflow object.
- TextFlow(Node… c): Create a new textflow object with specified nodes.
Commonly Used Methods:
| Method | Explanation |
|---|---|
| getLineSpacing() | Returns the line spacing of the text flow |
| getTextAlignment() | Returns the text alignment of the text flow |
| setLineSpacing(double s) | Set line spacing of the text flow . |
| setTextAlignment(TextAlignment v) | Sets the text alignment of the text flow. |
Below programs illustrate the use of TextFlow class:
- Java program to create a TextFlow and add text object to it: In this program we will create a TextFlow named text_flow and two Text named text_1 and text_2. Set the fill and font using setFill() and setFont(). We will add the text to the text_flow using the getChildren().add() function. Add the text_flow to the scene and scene to the stage. Call the show() function to display the final results.
// Java program to create a TextFlow and// add text object to it .importjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.event.ActionEvent;importjavafx.scene.paint.*;importjavafx.scene.text.*;importjavafx.scene.web.*;importjavafx.scene.layout.*;importjavafx.scene.shape.*;publicclassTextFlow_0extendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("TextFlow");// create TextFlowTextFlow text_flow =newTextFlow();// create textText text_1 =newText("Lazyroar\n");// set the text colortext_1.setFill(Color.RED);// set font of the texttext_1.setFont(Font.font("Verdana",25));// create textText text_2 =newText("The computer science portal for geeks");// set the text colortext_2.setFill(Color.BLUE);// set font of the texttext_2.setFont(Font.font("Helvetica", FontPosture.ITALIC,15));// add text to textflowtext_flow.getChildren().add(text_1);text_flow.getChildren().add(text_2);// create a sceneScene scene =newScene(text_flow,400,300);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:
- Java program to create a TextFlow and add text object to it, set text Alignment and set line spacing of the text flow: In this program we will create a TextFlow named text_flow and two Text named text_1 and text_2. Set the fill and font using setFill() and setFont(). Set TextAlignment using setTextAlignment() and set the line spacing using the setLineSpacing() function. Add the text to the text_flow using the getChildren().add() function. Add the text_flow to the Vbox. Add the vbox scene and the scene to the stage. Call the show() function to display the final results.
// Java program to create a TextFlow and// add text object to it, set text Alignment// and set line spacing of the text flow.importjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.scene.layout.*;importjavafx.scene.paint.*;importjavafx.scene.text.*;importjavafx.geometry.*;importjavafx.scene.layout.*;importjavafx.scene.shape.*;publicclassTextFlow_1extendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("FlowPane");// create TextFlowTextFlow text_flow =newTextFlow();// create textText text_1 =newText("Lazyroar\n");// set the text colortext_1.setFill(Color.GREEN);// set font of the texttext_1.setFont(Font.font("Verdana",25));// create textText text_2 =newText("The computer science portal for geeks");// set the text colortext_2.setFill(Color.BLUE);// set font of the texttext_2.setFont(Font.font("Helvetica", FontPosture.ITALIC,15));// add text to textflowtext_flow.getChildren().add(text_1);text_flow.getChildren().add(text_2);// set text Alignmenttext_flow.setTextAlignment(TextAlignment.CENTER);// set line spacingtext_flow.setLineSpacing(20.0f);// create VBoxVBox vbox =newVBox(text_flow);// set alignment of vboxvbox.setAlignment(Pos.CENTER);// create a sceneScene scene =newScene(vbox,400,300);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:
Note: The above programs might not run in an online IDE please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/text/TextFlow.html




