为了账号安全,请及时绑定邮箱和手机立即绑定

ScrollPane javafx 自动滚动(将 vvalue 设置为 1.0)

ScrollPane javafx 自动滚动(将 vvalue 设置为 1.0)

万千封印 2021-07-03 10:41:22
所以,情况是......我在滚动窗格中有一个 vbox。我将 hbox 添加到 vbox 中,然后vbox.setVvalue(1.0)在每次插入后调用。然而,假设有 5 个 hbox,滚动条只会使最后一个可见项目是第 4 个 hbox - 在当前看到的下方有一个 hbox(需要向下滚动才能看到)。我找到了一个解决方案,它将滚动窗格的 vvalue 属性绑定到 vbox 的 height 属性,如下所示:scrollPane.vvalueProperty().bind(vbox.heightProperty())我假设每次更改 vbox 高度时(即添加新的 hbox 时)都会将 vvalue 更改为最大值。但是,我仍然想提高我的知识以及为什么第一个(在每次插入后设置滚动窗格的 vvalue)与绑定属性不同。谢谢!
查看完整描述

1 回答

?
德玛西亚99

TA贡献1770条经验 获得超3个赞

设置 newvvalue发生在由修改引起的布局传递之前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();

}


查看完整回答
反对 回复 2021-07-14
  • 1 回答
  • 0 关注
  • 491 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信