JavaFX - 使 ScrollPane 自动滚动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20333396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaFX - Make ScrollPane scroll automatically
提问by user3029101
I have a Label inside a ScrollPane. I am updating the label in a loop (In another thread). How can I update the ScrollPane so it scrolls down (not sideways, this will be done manually) if the user doesnt hold it at a position? Is there a setter for it?
我在 ScrollPane 中有一个标签。我正在循环更新标签(在另一个线程中)。如果用户没有将其保持在某个位置,我如何更新 ScrollPane 使其向下滚动(不是侧向滚动,这将手动完成)?有没有适合它的设置器?
采纳答案by user3029101
@Math thanks , that worked!
@Math 谢谢,成功了!
@FXML private ScrollPane scroll; //this must match the fx:id of the ScrollPane element or be the scrollpane object
scroll.setVvalue(1.0); //1.0 means 100% at the bottom
And I solved the problem "must be looking at the tab" with this part of code
我用这部分代码解决了“必须查看标签”的问题
tab.setOnSelectionChanged(new EventHandler<Event>() {
@Override
public void handle(Event arg0) {
ScrollPane.setVvalue(1.0);
}
});
回答by Math
To set the ScrollPane to the bottom automatically set the vvalue
of the ScrollPane element, like this:
要将 ScrollPane 设置为底部,请自动设置vvalue
ScrollPane 元素的 ,如下所示:
@FXML private ScrollPane scroll; //this must match the fx:id of the ScrollPane element
scroll.setVvalue(1.0); //1.0 means 100% at the bottom
回答by Alberto Trindade Tavares
If you are using either HBox or VBox within the ScrollPane, try the following:
如果您在 ScrollPane 中使用 HBox 或 VBox,请尝试以下操作:
hBox.heightProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue observable, Object oldvalue, Object newValue) {
scrollPane.setHvalue((Double)newValue );
}
});
回答by brack11
This works in my code:
这适用于我的代码:
scrollPane.vvalueProperty().bind(mainGrid.heightProperty());
in my case scrollPane contains mainGrid
在我的情况下,scrollPane 包含 mainGrid
回答by Francesco Menzani
To scroll the scroll pane to the bottom completely, set its vvalue
propertyto 1.0
:
要将滚动窗格完全滚动到底部,请将其vvalue
属性设置为1.0
:
scrollPane.setVvalue(1D);
Note this may not work when called after resizing the scroll pane's content.
In such a case, if delaying the call via Platform.runLater()
doesn't fix the problem, consider setting the property's value in response to the content height
propertyinvalidation event:
请注意,在调整滚动窗格内容的大小后调用这可能不起作用。
在这种情况下,如果延迟调用 viaPlatform.runLater()
不能解决问题,请考虑设置属性值以响应内容height
属性失效事件:
vBox.heightProperty().addListener(observable -> scrollPane.setVvalue(1D));
回答by Martin Pfeffer
This should do the trick:
这应该可以解决问题:
// adding a new row
vbox_event.addEventRow((Integer) null, null);
// scroll to bottom
Platform.runLater(new Runnable() {
@Override
public void run() {
scrollpane.setVvalue(1.0);
}
});