I'm saving the state of two scrollbars and loading it with these two methods:
private void saveScrollPosition() {
scrollY = scrollPane.getVerticalScrollBar().getValue();
scrollX = scrollPane.getHorizontalScrollBar().getValue();
System.out.println("Saved: " + scrollX + "," + scrollY);
}
private void loadScrollPosition() {
scrollPane.getVerticalScrollBar().setValue(scrollY);
scrollPane.getHorizontalScrollBar().setValue(scrollX);
System.out.println("Should load: " + scrollX + "," + scrollY);
System.out.println("Loaded: " + scrollPane.getHorizontalScrollBar().getValue() + "," + scrollPane.getVerticalScrollBar().getValue());
}
The horizontal scrollbar is doing what it is supposed to do but the vertical is always set to 90.
Console:
Saved: 185,882
Should load: 185,882
Loaded: 185,90
Found a tip in a forum to create a new Runnable method and start it with
SwingUtilities.invokeLater(doScroll);
but it didn't work out.
I wonder what I'm doing wrong...
I had to update my contentPane first. Then the vertical scrollbar got updated to the correct values. Idk why it worked with the horizontal scrollbar...
Before:
loadScrollPosition();
contentPane.updateUI();
After
contentPane.updateUI();
loadScrollPosition();
Hope this helps