Glavo
Glavo

Reputation: 503

How to make left-aligned TextFlow in the center of the window in JavaFX

How to make text in TextFlow justify left, but the TextFlow is in the center of the window?

I try to implement it with VBox, StackPane and BorderPane, but they can only align the text in the center, or make the TextFlow to the left of the window.

The effect I need is similar to IDEA: The effect i need

But the effect I achieved is like this:

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        TextFlow text = new TextFlow(
            new Text("Search Everywhere\n"),
            new Text("Project View\n"),
            new Text("Go to File\n")
        );
        text.setTextAlignment(TextAlignment.LEFT);
        VBox root = new VBox(text);
        root.setAlignment(Pos.CENTER);

        primaryStage.setScene(new Scene(root, 400, 400));
        primaryStage.show();
     }
  }

enter image description here

Upvotes: 1

Views: 950

Answers (2)

Glavo
Glavo

Reputation: 503

Thanks for c0der tips, I found FlowPane can easily achieve this effect:

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        TextFlow text = new TextFlow(
            new Text("Search Everywhere\n"),
            new Text("Project View\n"),
            new Text("Go to File\n")
        );
        text.setTextAlignment(TextAlignment.LEFT);
        FlowPane root = new FlowPane(text);
        root.setAlignment(Pos.CENTER);

        primaryStage.setScene(new Scene(root, 400, 400));
        primaryStage.show();
    }
}

effect

Upvotes: 1

purring pigeon
purring pigeon

Reputation: 4209

I am not sure this is exactly what you are looking for, but should put you in the right direction. I added color so you can see where one control ends and the other continues.

To make this work you need to ensure that your TextFlow doesn't size bigger than what you want, otherwise it will not give you the expected behavior. In this instance I choose 200x200 and you will see it center in the window.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;

public class Sample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        TextFlow tf = new TextFlow();
        tf.setStyle("-fx-background-color: red;");
        tf.setTextAlignment(TextAlignment.LEFT);
        tf.setMaxSize(200, 200);

        StackPane sp = new StackPane(tf);
        sp.setStyle("-fx-background-color: blue;");

        Text t1 = new Text("This is line one, left justified" + System.lineSeparator());
        Text t2 = new Text("This is line two, left justified"+ System.lineSeparator());
        Text t3 = new Text("This is line three, left justified"+ System.lineSeparator());
        Text t4 = new Text("This is line four, left justified"+ System.lineSeparator());
        tf.getChildren().addAll(t1, t2, t3, t4);

        Scene scene = new Scene(sp);
        primaryStage.setScene(scene);
        primaryStage.setWidth(600);
        primaryStage.setHeight(600);

        primaryStage.show();

    }

}

Upvotes: 0

Related Questions