【问题标题】:ScrollPane javafx auto scrolling (setting vvalue to 1.0) only scrolls to the item before the lastScrollPane javafx 自动滚动(将 vvalue 设置为 1.0)只滚动到最后一个项目之前
【发布时间】:2019-01-18 13:17:29
【问题描述】:

所以,情况是……我在滚动窗格中有一个 vbox。我将 hbox 添加到 vbox 中,然后在每次插入后调用 vbox.setVvalue(1.0)

但是说,有 5 个 hbox,滚动条只会使最后一个可见项目是第 4 个 hbox - 在当前看到的内容下方有一个 hbox(需要向下滚动才能看到)。

我找到了一个解决方案,将滚动窗格的 vvalue 属性绑定到 vbox 的 height 属性,如下所示:scrollPane.vvalueProperty().bind(vbox.heightProperty()) 我假设每次更改 vbox 高度时都会将 vvalue 更改为最大值(即,当新的 hbox 是添加)。

但是,我仍然想提高我的知识以及为什么第一个(在每次插入后设置滚动窗格的 vvalue)与绑定属性不同。谢谢!

【问题讨论】:

    标签: java javafx autoscroll scrollpane


    【解决方案1】:

    设置新的vvalue 发生在由修改VBox 引起的布局传递之前,但结果在布局传递之前应用。由于视口中显示的顶部 y 坐标的公式是

    top = max(0, vvalue * (contentHeight - viewportHeight))
    

    在布局过程中,内容的左上角保持不变,您可以在视口底部看到旧内容的底部。

    要解决此问题,您可以使用手动触发 ScrollPane 上的布局传递

    scrollPane.applyCss();
    scrollPane.layout();
    

    示例

    @Override
    public void start(Stage primaryStage) {
        VBox content = new VBox();
        ScrollPane scrollPane = new ScrollPane(content);
        VBox.setVgrow(scrollPane, Priority.ALWAYS);
        Button button = new Button("fill");
        button.setOnAction(evt -> {
            for (int i = 0; i < 20; i++) {
                content.getChildren().add(new Text(Integer.toString(i)));
            }
            System.out.println("content size before layout: " + content.getHeight());
    
            // manually layout scrollPane
            scrollPane.applyCss();
            scrollPane.layout();
    
            System.out.println("content size after layout: " + content.getHeight());
            scrollPane.setVvalue(1d);
        });
    
        Scene scene = new Scene(new VBox(button, scrollPane), 200, 200);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    

    【讨论】:

    • 很难理解,但明白了要点
    猜你喜欢
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 2017-01-18
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多