JavaFX - Make ScrollPane scroll automatically

19,717

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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

Solution 5

This works in my code:

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

in my case scrollPane contains mainGrid

Share:
19,717
user3029101
Author by

user3029101

Updated on July 15, 2022

Comments

  • user3029101
    user3029101 almost 2 years

    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?

  • user3029101
    user3029101 over 10 years
    This is not working. (Offtopic : Why use FXML?(I use java and CSS sometimes)). Actually , it works only if I am at the tab.(If I have opened the specific tab)
  • Math
    Math over 10 years
    The FXML file is where you build your Scene, then in the Controller.java you manipulate the elements associating a variable via the element's fx:id. If it is working only if you're in the tab it means you have to put the .setVvalue() somewhere else, depending on when you want to fire it, probably somewhere in the thread along with where you write in your Label.
  • user3029101
    user3029101 over 10 years
    I build my scene with pure java
  • Math
    Math over 10 years
    Well.. you also have this option, but I discarded this since I knew about SceneBuilder. It works independently on your IDE and automatizes the process of building the FXML files. In my opinion, it worths to at least take a look at it.
  • user3029101
    user3029101 over 10 years
    OK. Got it . I have an idea. Since this thing works only if I am looking at the tab , I will add a listener (tab change/select event) and then set the vvalue. I came across different code snippets. None of them worked. I have a TabPane
  • Math
    Math over 10 years
    Actually there wasn't much information in your question saying what event should fire the scroll, so it was a kind of impossible to me to determine it by myself.
  • user3029101
    user3029101 over 10 years
    Thanks anyway. The whole problem is the tab , but I thought I should include what I did , in case someone finds it useful!
  • spongebob
    spongebob over 6 years
    This only works by accident when the box's height is greater than or equal to 1.0, that is the default maximum value of the hvalue property, which in turn should be vvalue. So an invalidation listener is sufficient. See my answer for a more correct solution.