I have a JScrollPane with a very high JPanel inside, that is changed dynamically, items being appended at its end. What I want, is to scroll to the bottom of aforementioned JScrollPane in order for the newly appended items to be visible instantly on addition (they are not appended to the scroll pane directly, but to its JPanel, and are private objects, so cannot be referenced.
How can I simply have that scroll pane scroll to the very bottom? Thanks in advance!
This is how I scroll down programmatically.
I like how it scrolls to the bottom rather smoothly instead of jumping there immediately.
scrollRectToVisible(...) and scrollBar.setValue(...) are the general solutions.
You may be interested in Scrolling a Form which ensures that when you tab to a component the form will scroll automatically to make sure the the component will be visible in the scrollpane. Behind the scenes it uses scrollRectToVisible().
A simple way to move the scrollbar all the way to the bottom is to set its value to 100 like this:
This causes it to move to the bottom of the viewport. You can add this after the component is added to the panel.
JComponent.scrollRectToVisible(Rectangle)
. Call that on theJPanel
instance.E.G.
Screen shot
E.G. 2
This e.g. is based on Vincent's answer, to use
JScrollPane.getVerticalScrollBar()
.setValue(height)
. Whereheight
is the preferred height of the panel in pixels.