4

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!

Jomy
  • 514
  • 6
  • 22
  • 1
    minor: the other labels stop wrapping because the vbox stops resizing itself at the min of the flowPane (as you verify by setting the flow's min to 0: now the other labels wrap further). No idea how to fix the main issue, though .. might be a bug, some destructive interaction of vbox and flowpane – kleopatra Mar 26 '21 at 12:57
  • ... to clarify my last half-sentence: same behavior without scrollPane - that's why I assume it's an issue between vbox and flowpane :) – kleopatra Mar 26 '21 at 13:03
  • @kleopatra Thank you. I tried it with a HBox instead of a FlowPane, which did work. A FlowPane would get me better results, but I guess I will have to settle for the HBox. – Jomy Mar 26 '21 at 14:11
  • 1
    I stripped out everything until it was just a FlowPane in the Scene. I added another Label to the FlowPane. The labels do not wrap, but the FlowPane must be calculating a minWidth which is the width of the largest child node. I tried binding the maxWidth of FlowPane to the width of VBox when it was still there, and it overrode it to take the full width of the long Label. – DaveB Mar 26 '21 at 21:57

0 Answers0