ScrollPane未按要求显示FlowPane内容

时间:2017-11-03 00:31:14

标签: java javafx javafx-8 scrollpane flowpane

我在JavaFX 8中使用ScrollPane遇到一些困难,根据需要显示滚动条。我目前正在做的只是创建一个包含x个元素的FlowPane,并将其设置为ScrollPane的内容。

当我垂直于FlowPane的方向收缩时,会出现问题。当元素开始换行并越界时,滚动条不会出现。当我平行收缩时,这不会发生。我有一个小的Java程序来举例解释这个问题。

Start

Shrinking Parallel

Shrinking Perpendicular

Shrinking PerpendicularExaggerated Shrinking Parallel Exaggerated

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();
             flow.setStyle("-fx-border-color: red");
             addPanes(flow, 16);

             ScrollPane scroll = new ScrollPane(flow);
             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);
             }
       }
}

3 个答案:

答案 0 :(得分:2)

看看下面的代码并告诉我这是否是您想要实现的目标。我仍然不确定导致问题的原因,我将不得不查看ScrollPane的文档来查找。我怀疑是setFitToWidth&amp; setFitToHeight方法。虽然我仍然相信它不是一个错误。

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
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();
        flow.setStyle("-fx-border-color: red");

        addPanes(flow, 16);

        ScrollPane scroll = new ScrollPane(flow);
        scroll.setStyle("-fx-border-color: green");

        // Apparently this cause the issue here.
        // scroll.setFitToHeight(true);
        // scroll.setFitToWidth(true);


        // Instead just make the flow pane take the dimensions of the ScrollPane
        // the -5 is to not show the Bars when both of panes have the same dimensions  
        flow.prefWidthProperty().bind(Bindings.add(-5, scroll.widthProperty()));
        flow.prefHeightProperty().bind(Bindings.add(-5, scroll.heightProperty()));

        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++) {
            HBox filler = new HBox();
            filler.setStyle("-fx-border-color: black");
            filler.setPrefSize(100, 100);
            root.getChildren().add(filler);
        }
    }
}

查看ScrollPane的文档,具体而言setFitToHeight您会发现:

  

物业描述:   如果为true,并且包含的​​节点是a   可调整大小,然后节点将保持调整大小以匹配高度   ScrollPane的视口。如果包含的节点不是Resizable,   这个值被忽略了。

因为ScrollPane中的节点将保持调整大小以匹配ScrollPane视口的宽度和高度,这就是为什么Vertical ScrollBar永远不会出现。

答案 1 :(得分:1)

您可以添加以下代码,以便始终显示垂直滚动条。

scroll.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);

答案 2 :(得分:0)

当计算出ScrollPane内部所需的FlowPane高度时,将传递-1的宽度值。然后,当流窗格的所有内容都放在一行中时,它将报告所需的高度。

作为一种解决方法,在这种情况下,您可以从上一次布局计算中传递宽度。

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 : 
                        /* When no width is specified, use the current contol size*/ 
                        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)->{
                 /* clearSizeCache */
                 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);
             }
       }
}