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
I just had the same problem. If somebody still has this problem, here is my situation:
My JScrollPane
gets constructed new every time and at this moment the maximum of the VerticalScrollBar
was set to 100, so it couldn't accept a higher value.
I fixed it by saving the maximum before constructing the new JScrollPane
and then set it to the new Object.
if (scrollPane != null) {
scrollPos = scrollPane.getVerticalScrollBar().getValue();
maxScrollPos = scrollPane.getVerticalScrollBar().getMaximum();
}
scrollPane = new JScrollPane(panel);
if (scrollPos != null && maxScrollPos != null) {
scrollPane.getVerticalScrollBar().setMaximum(maxScrollPos);
scrollPane.getVerticalScrollBar().setValue(scrollPos);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.