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 applicationĀ Ā Ā Āpublicvoidstart(Stage stage)Ā Ā Ā Ā{ĀĀĀ Ā Ā Ā Ā Ā Ā Ātry{ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// set title for the stageĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Āstage.setTitle("TextFlow");ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// create TextFlowĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ĀTextFlow text_flow =newTextFlow();ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// create textĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ĀText text_1 =newText("Lazyroar\n");ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// set the text colorĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ātext_1.setFill(Color.RED);ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// set font of the textĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ātext_1.setFont(Font.font("Verdana",25));ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// create textĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ĀText text_2 =newText("The computer science portal for geeks");ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// set the text colorĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ātext_2.setFill(Color.BLUE);ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// set font of the textĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ātext_2.setFont(Font.font("Helvetica", FontPosture.ITALIC,15));ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// add text to textflowĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ātext_flow.getChildren().add(text_1);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ātext_flow.getChildren().add(text_2);ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// create a sceneĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ĀScene scene =newScene(text_flow,400,300);ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// set the sceneĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Āstage.setScene(scene);ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Āstage.show();Ā Ā Ā Ā Ā Ā Ā Ā}ĀĀĀ Ā Ā Ā Ā Ā Ā Ācatch(Exception e) {ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ĀSystem.out.println(e.getMessage());Ā Ā Ā Ā Ā Ā Ā Ā}Ā Ā Ā Ā}ĀĀĀ Ā Ā Ā// Main MethodĀ Ā Ā Āpublicstaticvoidmain(String args[])Ā Ā Ā Ā{ĀĀĀ Ā Ā Ā Ā Ā Ā Ā// launch the applicationĀ Ā Ā Ā Ā Ā Ā Ālaunch(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 applicationĀ Ā Ā Āpublicvoidstart(Stage stage)Ā Ā Ā Ā{ĀĀĀ Ā Ā Ā Ā Ā Ā Ātry{ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// set title for the stageĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Āstage.setTitle("FlowPane");ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// create TextFlowĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ĀTextFlow text_flow =newTextFlow();ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// create textĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ĀText text_1 =newText("Lazyroar\n");ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// set the text colorĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ātext_1.setFill(Color.GREEN);ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// set font of the textĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ātext_1.setFont(Font.font("Verdana",25));ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// create textĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ĀText text_2 =newText("The computer science portal for geeks");ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// set the text colorĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ātext_2.setFill(Color.BLUE);ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// set font of the textĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ātext_2.setFont(Font.font("Helvetica", FontPosture.ITALIC,15));ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// add text to textflowĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ātext_flow.getChildren().add(text_1);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ātext_flow.getChildren().add(text_2);ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// set text AlignmentĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ātext_flow.setTextAlignment(TextAlignment.CENTER);ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// set line spacingĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ātext_flow.setLineSpacing(20.0f);ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// create VBoxĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ĀVBox vbox =newVBox(text_flow);ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// set alignment of vboxĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Āvbox.setAlignment(Pos.CENTER);ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// create a sceneĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ĀScene scene =newScene(vbox,400,300);ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā// set the sceneĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Āstage.setScene(scene);ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Āstage.show();Ā Ā Ā Ā Ā Ā Ā Ā}ĀĀĀ Ā Ā Ā Ā Ā Ā Ācatch(Exception e) {ĀĀĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ĀSystem.out.println(e.getMessage());Ā Ā Ā Ā Ā Ā Ā Ā}Ā Ā Ā Ā}ĀĀĀ Ā Ā Ā// Main MethodĀ Ā Ā Āpublicstaticvoidmain(String args[])Ā Ā Ā Ā{ĀĀĀ Ā Ā Ā Ā Ā Ā Ā// launch the applicationĀ Ā Ā Ā Ā Ā Ā Ālaunch(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




