简体   繁体   English

JavaFX - 使ScrollPane自动滚动

[英]JavaFX - Make ScrollPane scroll automatically

I have a Label inside a ScrollPane. 我在ScrollPane中有一个Label。 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? 我如何更新ScrollPane,使其向下滚动(不是侧身,这将手动完成),如果用户没有将其保持在某个位置? Is there a setter for it? 它有一个二传手吗?

To set the ScrollPane to the bottom automatically set the vvalue of the ScrollPane element, like this: 要将ScrollPane设置为底部,请自动设置ScrollPane元素的vvalue ,如下所示:

@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

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 );  
    }
});

This works in my code: 这适用于我的代码:

scrollPane.vvalueProperty().bind(mainGrid.heightProperty());

in my case scrollPane contains mainGrid 在我的例子中,scrollPane包含mainGrid

To scroll the scroll pane to the bottom completely, set its vvalue property to 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 property invalidation event: 在这种情况下,如果通过Platform.runLater()延迟调用无法解决问题,请考虑设置属性的值以响应内容height属性失效事件:

vBox.heightProperty().addListener(observable -> scrollPane.setVvalue(1D));

@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);
    }        
});

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);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM