JavaFX-以正确的滚动方向将节点添加到ScrollPane [英] JavaFX - Adding nodes to ScrollPane with correct scrolling direction

查看:129
本文介绍了JavaFX-以正确的滚动方向将节点添加到ScrollPane的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的场景中有一个ScrollPane,想向其中添加一系列Node.确切的数量必须是动态的.我一直在用Label节点测试该概念,并且可以成功地向ScrollPane(当前为VBox)的内容中添加任意数量的Label.

I have a ScrollPane in my scene and would like to add a series of Nodes to it. The exact amount must be dynamic. I've been testing the concept with Label nodes and can successfully add as many Labels as I want to the content of the ScrollPane (currently a VBox).

我的问题是弄清楚如何允许用户在充满节点的ScrollPane中正常滚动.内容与滚动条匹配.也就是说,当我向下滚动时,内容将向下移动.我向上滚动,内容向上滚动.因此,如果我有10个标签,当前仅可见五个标签,则查看其他五个标签的本能动作是向下滚动.不幸的是,这只是将整个内容向下移动,在顶部暴露空白空间并隐藏更多标签.

My problem is figuring out how to allow the user to scroll normally through the ScrollPane full of Nodes. The content matches pace with the scrollbar; that is, when I scroll down, the content moves down. I scroll up, the content scrolls up. So if I have 10 Labels, only five of which are currently visible, the instinctive action to see the other five would be to scroll down. Unfortunately, this just moves the entire content down, exposing empty space up top and hiding more of the Labels.

如果有帮助,我可以发布一些示例代码,但是我的攻击计划的要旨是:

I can post some sample code if that helps, but the gist of my plan of attack is this:

ScrollPane sp = new ScrollPane();
VBox content = new VBox();
sp.setContent(content);
for (int i = 0; i < 10; i++)
{
    Label label = new Label("Label " + i);
    content.setPrefHeight(content.getPrefHeight() + label.getPrefHeight());
    content.getChildren().add(label);
}

因为我使用的是VBox,所以标签相互堆叠(在Y轴上),这正是我想要的.随着添加更多标签,ScrollBar需要反映VBox增大的大小. 我尝试了几种方法来使它起作用,包括ScrollPane.setVmax()方法,这是可以的,但是仍然存在滚动方向将内容向错误方向移动的问题.我还尝试了EventHandlerEventFilter来相应地修改值,但仍未解决滚动不能将内容沿正确方向移动的问题.

Because I use a VBox, the Labels are stacked (in the Y axis) atop one another, which is what I want. As more Labels are added, the ScrollBar needs to reflect the increased size of the VBox. I've tried a few approaches to get this to work, including ScrollPane.setVmax() method, which is ok, but I still have the issue of the scroll direction moving the content in the wrong direction. I've also tried an EventHandler and EventFilter to modify values accordingly, still without solving the problem of scrolls not moving the content in the right direction.

答案似乎很简单,但即使经过很长的时间弄乱了代码,它仍然让我回避.

The answer seems so simple, yet the it continues to evade me, even after many long hours messing around with the code.

jewelsea建议使用ListView解决了我的问题.但是,我仍然对为什么使用ScrollPane表现不佳感到好奇.另外,我在ListView实现中遇到了奇怪的行为:我使用按钮将新的Label手动添加到视图中.添加新的Label后,视图将不时冻结.在更新滚动位置或显示新添加的Label之前,它会锁定几秒钟.我不知道这是否是因为视图管理着大约20个左右的Label实例,或者是否正在起着更阴险的作用.

jewelsea's suggestion of using a ListView solved my problem. However, I remain curious as to why using a ScrollPane did not fare so well. Additionally, I've run into odd behavior with the ListView implementation: I use a button to manually add a new Label to the view. Every now and then, the view will freeze up after adding a new Label. It locks up for a few seconds before either updating the scroll position or displaying the newly added Label. I don't know if this is because there are about 20 or so Label instances being managed by the view or if something more insidious is at play.

推荐答案

关于滚动窗格问题,我知道您要执行的操作是可能的,因为我正在执行非常相似的操作.根据您在上面提供的代码段,我建议您尝试一些尝试...

In regards to the scrollpane issues, I know what you're looking to do is possible because I'm doing something very similar. Based on the code snippet you gave above I would suggest trying few things...

  • 为ScrollPane提供首选的高度和宽度以供遵循.
  • 请勿对内容VBox设置任何高度调整偏好,因为它会随着内容的增长而增长.添加内容后,也无需更新其高度.
  • 使用VBox的 setAlignment 方法对齐 TOP_LEFT TOP_CENTER TOP_RIGHT 的内容.
  • li>
  • 在使用 getPrefHeight()方法时要小心,因为如果您以前没有使用过 setPrefHeight(),我相信它会返回 -1 .
  • Give the ScrollPane a preferred height and width to follow.
  • Don't give the content VBox any height sizing preference as it will grow with its contents. There should be no need to update its height after adding content either.
  • Use the VBox's setAlignment method to align its contents either TOP_LEFT, TOP_CENTER, or TOP_RIGHT.
  • Be cautious when using the getPrefHeight() method because if you haven't used setPrefHeight() beforehand I believe it will return -1.

希望这对您有所帮助!

这篇关于JavaFX-以正确的滚动方向将节点添加到ScrollPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆