I have a JScrollPane
whose viewport is a subclass of JPanel
. This panel is laid out with a BoxLayout
and upon user interaction the horizontal width of the components can change. When there are sufficient components such that the scroll bars are needed, changing the horizontal size of the component and then calling revalidate()
on the JPanel
subclass (the viewport view) will always cause the scroll pane to reset to (0,0).
I would like to keep the current view, so I've set up a component listener and on componentMoved()
I store the value at getLocation()
(this is all in the JPanel subclass or the viewport view). Immediately after calling revalidate()
I call setLocation()
with the stored location (on the event dispatch thread). However, that causes jumpiness where the view position is set to (0,0) then jumps immediately to the stored location. Is there a way to stop the JScrollPane
from going back to (0,0)? Is there a better way to tell it that the size of it's viewport view has changed?
I've tried to figure out where the (0,0)
is coming from by subclassing the JScrollBar
and overriding setValue()
and putting a breakpoint, but it's not being hit there (but is when I move the scroll wheel) so I assume it's being set internally. Does anyone know what might be happening?
Try another way to call the code after
revalidate()
buf beforerepaint()
.Alternatively you can try to call
panel.scrollRectToVisible(Rectangle)