JavaFX - 使ScrollPane自动滚动

use*_*101 8 java user-interface label scroll javafx

我在ScrollPane中有一个Label.我正在循环中更新标签(在另一个线程中).我如何更新ScrollPane,使其向下滚动(不是侧身,这将手动完成),如果用户没有将其保持在某个位置?它有一个二传手吗?

Mat*_*ath 13

要将ScrollPane设置为底部,会自动设置vvalueScrollPane元素,如下所示:

@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
Run Code Online (Sandbox Code Playgroud)


Alb*_*res 6

如果您在ScrollPane中使用HBox或VBox,请尝试以下操作:

hBox.heightProperty().addListener(new ChangeListener() {
    @Override
    public void changed(ObservableValue observable, Object oldvalue, Object newValue) {
        scrollPane.setHvalue((Double)newValue );  
    }
});
Run Code Online (Sandbox Code Playgroud)


bra*_*k11 6

这在我的代码中有效:

scrollPane.vvalueProperty().bind(mainGrid.heightProperty());
Run Code Online (Sandbox Code Playgroud)

就我而言,scrollPane 包含 mainGrid


Fra*_*ani 5

要将滚动窗格完全滚动到底部,请将其vvalue属性设置为1.0

scrollPane.setVvalue(1D);
Run Code Online (Sandbox Code Playgroud)

请注意,在调整滚动窗格内容的大小后调用这可能不起作用。
在这种情况下,如果延迟调用 viaPlatform.runLater()不能解决问题,请考虑设置属性值以响应内容height属性失效事件:

vBox.heightProperty().addListener(observable -> scrollPane.setVvalue(1D));
Run Code Online (Sandbox Code Playgroud)


use*_*101 4

@Math thanks , that worked!

@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
Run Code Online (Sandbox Code Playgroud)

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);
    }        
});
Run Code Online (Sandbox Code Playgroud)