JavaFX - Make ScrollPane scroll automatically

2019-02-17 00:23发布

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?

6条回答
Rolldiameter
2楼-- · 2019-02-17 01:05

@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

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);
    }        
});
查看更多
萌系小妹纸
3楼-- · 2019-02-17 01:07

To scroll the scroll pane to the bottom completely, set its vvalue property to 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:

vBox.heightProperty().addListener(observable -> scrollPane.setVvalue(1D));
查看更多
萌系小妹纸
4楼-- · 2019-02-17 01:08

To set the ScrollPane to the bottom automatically set the vvalue of the ScrollPane element, like this:

@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
查看更多
老娘就宠你
5楼-- · 2019-02-17 01:09

If you are using either HBox or VBox within the ScrollPane, try the following:

hBox.heightProperty().addListener(new ChangeListener() {
    @Override
    public void changed(ObservableValue observable, Object oldvalue, Object newValue) {
        scrollPane.setHvalue((Double)newValue );  
    }
});
查看更多
等我变得足够好
6楼-- · 2019-02-17 01:10

This works in my code:

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

in my case scrollPane contains mainGrid

查看更多
淡お忘
7楼-- · 2019-02-17 01:11

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);
    }
});
查看更多
登录 后发表回答