When the required FlowPane height inside the ScrollPane is calculated, a width value of -1 is passed. Then the flow panel will report the required height when all its contents fit on one line.
As a workaround, you can pass the width from the last layout calculation in this case.
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.ScrollPane; import javafx.scene.layout.FlowPane; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class Main extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) throws Exception { FlowPane flow = new FlowPane() { @Override protected double computeMinHeight(double width) { double minHeight = super.computeMinHeight(width != -1 ? width : getWidth()); return minHeight; } }; flow.setStyle("-fx-border-color: red"); addPanes(flow, 16); ScrollPane scroll = new ScrollPane(flow); flow.maxWidthProperty().bind(scroll.widthProperty()); scroll.widthProperty().addListener((observable, oldValue, newValue)->{ flow.requestLayout(); }); scroll.setStyle("-fx-border-color: green"); scroll.setFitToHeight(true); scroll.setFitToWidth(true); Scene scene = new Scene(scroll, 450, 450); primaryStage.setScene(scene); primaryStage.show(); } public void addPanes(FlowPane root, int panes) { for(int i = 0; i < panes; i++) { StackPane filler = new StackPane(); filler.setStyle("-fx-border-color: black"); filler.setPrefSize(100, 100); root.getChildren().add(filler); } } }
source share