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:

  1. TextFlow(): Create a new textflow object.
  2. 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:

  1. 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to create a TextFlow and 
    // add text object to it .
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.event.ActionEvent;
    import javafx.scene.paint.*;
    import javafx.scene.text.*;
    import javafx.scene.web.*;
    import javafx.scene.layout.*;
    import javafx.scene.shape.*;
      
    public class TextFlow_0 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
      
            try {
      
                // set title for the stage
                stage.setTitle("TextFlow");
      
                // create TextFlow
                TextFlow text_flow = new TextFlow();
      
                // create text
                Text text_1 = new Text("GeeksforGeeks\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 = new Text("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 = new Scene(text_flow, 400, 300);
      
                // set the scene
                stage.setScene(scene);
      
                stage.show();
            }
      
            catch (Exception e) {
      
                System.out.println(e.getMessage());
            }
        }
      
        // Main Method
        public static void main(String args[])
        {
      
            // launch the application
            launch(args);
        }
    }

    chevron_right

    
    

    Output:

  2. 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to create a TextFlow and 
    // add text object to it, set text Alignment
    // and set line spacing of the text flow.
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.scene.layout.*;
    import javafx.scene.paint.*;
    import javafx.scene.text.*;
    import javafx.geometry.*;
    import javafx.scene.layout.*;
    import javafx.scene.shape.*;
      
    public class TextFlow_1 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
      
            try {
      
                // set title for the stage
                stage.setTitle("FlowPane");
      
                // create TextFlow
                TextFlow text_flow = new TextFlow();
      
                // create text
                Text text_1 = new Text("GeeksforGeeks\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 = new Text("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 = new VBox(text_flow);
      
                // set alignment of vbox
                vbox.setAlignment(Pos.CENTER);
      
                // create a scene
                Scene scene = new Scene(vbox, 400, 300);
      
                // set the scene
                stage.setScene(scene);
      
                stage.show();
            }
      
            catch (Exception e) {
      
                System.out.println(e.getMessage());
            }
        }
      
        // Main Method
        public static void main(String args[])
        {
      
            // launch the application
            launch(args);
        }
    }

    chevron_right

    
    

    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



My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.