JavaFX - Make ScrollPane scroll automatically
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
user3029101
Updated on July 15, 2022Comments
-
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 over 10 yearsThis 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 over 10 yearsThe
FXML
file is where you build your Scene, then in theController.java
you manipulate the elements associating a variable via the element'sfx: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 over 10 yearsI build my scene with pure java
-
Math over 10 yearsWell.. 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 theFXML
files. In my opinion, it worths to at least take a look at it. -
user3029101 over 10 yearsOK. 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 over 10 yearsActually 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 over 10 yearsThanks anyway. The whole problem is the tab , but I thought I should include what I did , in case someone finds it useful!
-
spongebob over 6 yearsThis only works by accident when the box's height is greater than or equal to
1.0
, that is the default maximum value of thehvalue
property, which in turn should bevvalue
. So an invalidation listener is sufficient. See my answer for a more correct solution.