ScrollPane中的JavaFX FlowPane,动态调整滚动窗格的内容和大小

时间:2018-08-13 15:24:01

标签: java javafx

我目前有一个带有FlowPane的ScrollPane作为内容。目前,FlowPane初始化时没有子节点,固定宽度和pref / min高度(但没有最大高度)。

在运行时将项目添加到FlowPane时(我单击了一些UI元素,并且向FlowPane添加了一些内容),但在FlowPane的添加项不再适合的情况下,ScrollPane应该调整其高度。

我不明白如何设置flowPane和ScrollPane的高度,以使其可行-如果这是开始的问题。目前,只要FlowPane的添加项不适合其初始高度,就会添加内容,但不可见。属于ScrollPane的滚动条从不调整其高度-如果确实如此,我可以向下滚动并查看内容。

假设我有一个具有一定宽度和高度的ScrollPane,一些视口宽度/高度和一个具有某些宽度/高度的FlowPane-最小/首选/最大尺寸的设置应该是什么?如何使scrollPane调整其滚动条行为或使内容可见?

ScrollPane的setFitToHeight已设置为true,似乎没有任何改变。

import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class FlowPaneTest extends Application
{

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

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

        // borderPane rootPane
        BorderPane borderPane = new BorderPane();
        borderPane.setMinSize(600, 600);
        borderPane.setPrefSize(600, 600);
        borderPane.setMaxSize(600, 600);
        // container for the two scrollPanes below
        FlowPane flow = new FlowPane();
        borderPane.setRight(flow);

        // two scrollPanes, each should resize it's height (width should be fixed) if
        // children are added beyond it's current height
        ScrollPane top = new ScrollPane();
        ScrollPane bottom = new ScrollPane();

        FlowPane scrollPaneContent = new FlowPane();
        top.setContent(scrollPaneContent);
        bottom.setContent(scrollPaneContent);

        flow.getChildren().add(top);
        flow.getChildren().add(bottom);

        borderPane.setOnMouseClicked(new EventHandler<Event>()
        {

            @Override
            public void handle(Event event)
            {

                Label l = new Label("test");
                l.setMinSize(100, 100);
                l.setPrefSize(100, 100);
                l.setMaxSize(100, 100);

                scrollPaneContent.getChildren().add(l);

            }
        });

        // size settings

        int width = 300, height = 300;

        top.setHvalue(0.5);

        top.setMinViewportHeight(height);
        top.setPrefViewportHeight(height);
        top.setMinViewportWidth(width);
        top.setPrefViewportWidth(width);
        top.setHbarPolicy(ScrollBarPolicy.ALWAYS);
        top.setFitToHeight(true);

        top.setMinSize(width, height);
        top.setPrefSize(width, height);
        top.setMaxWidth(width);

        scrollPaneContent.setMinSize(width, height);
        scrollPaneContent.setPrefSize(width, height);
        scrollPaneContent.setMaxWidth(width);
        scrollPaneContent.setPrefHeight(height);

        bottom.setMinSize(width, height);
        bottom.setPrefSize(width, height);
        bottom.setMaxWidth(width);
        bottom.setHvalue(0.5);

        bottom.setMinViewportHeight(height);
        bottom.setPrefViewportHeight(height);
        bottom.setMinViewportWidth(width);
        bottom.setPrefViewportWidth(width);
        bottom.setHbarPolicy(ScrollBarPolicy.ALWAYS);
        top.setFitToHeight(true);
        bottom.setFitToHeight(true);

        // stage
        Scene scene = new Scene(borderPane, 600.0, 600.0);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

2 个答案:

答案 0 :(得分:0)

尝试指定ScrollPane的高度和宽度并添加此行

scrollPane.setFitToWidth(true);

答案 1 :(得分:0)

最后得到类似于以下这段丑陋的代码的代码-侦听窗格中子级的数量,并在每次向子级列表中添加某些内容时增加其大小:

curl http://localhost:3000/posts -i -X POST -d "title=Bigdadi Rules Posts&link=http://www.foo.com&upvotes=2"

有效,但感觉像是非正统的骇客。真的没有常规的方法可以这样做吗?

相关问题