JavaFX – 使ScrollPane自动滚动

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

要将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 

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

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

@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 

我用这部分代码解决了“必须查看选项卡”的问题

 tab.setOnSelectionChanged(new EventHandler() { @Override public void handle(Event arg0) { ScrollPane.setVvalue(1.0); } }); 

这适用于我的代码:

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

在我的例子中,scrollPane包含mainGrid

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

 scrollPane.setVvalue(1D); 

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

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

这应该是诀窍:

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