Jomy
Jomy

Reputation: 558

Labels not wrapping text correctly in ScrollPane

I have a problem when I add a FlowPane to another pane in a ScrollPane.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ScrollProblem extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        ScrollPane sc = new ScrollPane();
        VBox vbox = new VBox();
        sc.setContent(vbox);
        sc.setFitToWidth(true);
        sc.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);

        vbox.getChildren().addAll(
                new Lbl("Some text"),
                new Lbl("Some longer text that surpasses the width of the window"),
                // When I remove this FlowPane from the code, then everything works. When I add it
                // to the code, then the Labels do not wrap correctly.
                new FlowPane(
                        new Lbl("A flowpane containing some other text")
                )
        );


        Scene scene = new Scene(sc);
        primaryStage.setScene(scene);
        primaryStage.setWidth(200);
        primaryStage.setHeight(400);
        primaryStage.show();
    }

    private class Lbl extends Label {
        public Lbl(String text) {
            super(text);
            this.setWrapText(true);
        }
    }
}

This is how it looks (and how it should look) like without the flow pane:

How it should look

When I resize this window, the text still wraps correctly.

This is how it looks like with the flow pane:

enter image description here

As you can see, the label inside the flow pane does not wrap. Also, when I resize the window, the other labels do not wrap anymore:

enter image description here

Thank you in advance!

Upvotes: 4

Views: 429

Answers (0)

Related Questions