How to set alignment to text in text flow layout?

To create rich text contents in our applications JavaFX provides a special layout called text flow represented by the javafx.scene.layout.TextFlow class. Using this you ca lay out multiple text nodes in a single text flow.

Since they are separate nodes, you can set different fonts to them. If you try to add nodes other than text to this layout, they will be treated as embedded objects and are simply inserted between the text.

Setting the text alignment −

The textAlignment property of the TextFlow class specifies the horizontal alignment of the text in the layout. You can set the value to this property using the setTextAlignment() method. This method accepts four values −

  • TextAlignment.RIGHT

  • TextAlignment.LEFT

  • TextAlignment.CENTER

  • TextAlignment.JUSTIFY

To set the desired alignment to the text flow, invoke this method by passing the appropriate value.


import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
public class TextFlow_Wrap extends Application {
   public void start(Stage stage) throws FileNotFoundException {
      //Creating a text object
      Text text1 = new Text("Welcome To Tutorialspoint");
      Font font1 = Font.font("Brush Script MT", FontWeight.BOLD, 50);
      //Setting the color of the text
      Text text2 = new Text(" We provide free tutorials for readers in various technologies ");
      text2.setFont(new Font("Algerian", 30));
      Text text3 = new Text("We believe in easy learning");
      //Setting font to the text
      Font font2 = Font.font("Ink Free", FontWeight.BOLD, 35);
      //Creating the text flow
      TextFlow textFlow = new TextFlow();
      //Wrapping the content of the text flow
      textFlow.getChildren().addAll(text1, text2, text3);
      //Setting padding to the text flow
      textFlow.setPadding(new Insets(15, 15, 15, 15));
      //Setting the stage
      Group root = new Group(textFlow);
      Scene scene = new Scene(root, 595, 250, Color.BEIGE);
      stage.setTitle("Text Flow");
   public static void main(String args[]){


Updated on: 19-May-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started