How to create a text flow layout in JavaFX?


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 can layout 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.

This class has two properties −

  • lineSpacing − This property (double) is used to specify the space between the text objects. You can set value to this property using the setLineSpacing() method.

  • textAlignment − This property is used to specify the alignment of the text objects in the pane. You can set the value to this property using the setTextAlignment() method. To this method you can pass four values − CENTER, JUSTIFY, LEFT, RIGHT.

Example

import java.io.FileNotFoundException;
import javafx.application.Application;
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 TextFlowExample extends Application {
   public void start(Stage stage) throws FileNotFoundException {
      //Creating a text object
      Text text1 = new Text(30.0, 110.0, "Hi ");
      //Setting the font
      Font font1 = Font.font("Brush Script MT", FontWeight.BOLD, 75);
      text1.setFont(font1);
      //Setting the color of the text
      text1.setFill(Color.CORAL);
      text1.setStrokeWidth(1);
      text1.setStroke(Color.CHOCOLATE);
      Text text2 = new Text(40.0, 110.0, "Welcome To");
      Font font2 = Font.font("Verdana", FontWeight.LIGHT, 25);
      text2.setFont(font2);
      //Setting the color of the text
      text2.setFill(Color.YELLOWGREEN);
      text2.setStrokeWidth(1);
      text2.setStroke(Color.DARKRED);
      Text text3= new Text(50.0, 110.0, "Tutorialspoint");
      Font font3 = Font.font("Kunstler Script", FontWeight.BOLD, 80);
      text3.setFont(font3);
      //Setting the color of the text
      text3.setFill(Color.CORNFLOWERBLUE);
      text3.setStrokeWidth(1);
      text3.setStroke(Color.CRIMSON);
      Text text4 = new Text(" We provide free tutorials for readers in various technologies ");
      text4.setFont(new Font("Algerian", 30));
      text4.setFill(Color.DARKGOLDENROD);
      Text text5 = new Text("We believe in easy learning");
      //Setting font to the text
      text5.setFont(new Font("Ink Free",30));
      text5.setFill(Color.MEDIUMVIOLETRED);
      //Creating the text flow
      TextFlow textFlow = new TextFlow();
      textFlow.setPrefWidth(595);
      textFlow.getChildren().addAll(text1, text2, text3, text4, text5);
      //Setting the stage
      Group root = new Group(textFlow);
      Scene scene = new Scene(root, 595, 250, Color.BEIGE);
      stage.setTitle("Text Flow Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

Updated on: 19-May-2020

176 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements