简体   繁体   English

垂直滚动条 setValue 始终将其设置为 90

[英]Vertical scrollbar setValue always sets it to 90

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.水平滚动条正在做它应该做的事情,但垂直总是设置为 90。

Console:安慰:
Saved: 185,882已保存:185,882
Should load: 185,882应加载:185,882
Loaded: 185,90已加载:185,90

Found a tip in a forum to create a new Runnable method and start it with在论坛中找到了一个技巧来创建一个新的 Runnable 方法并开始它

SwingUtilities.invokeLater(doScroll);

but it didn't work out.但它没有成功。

I wonder what I'm doing wrong...我想知道我做错了什么......

I had to update my contentPane first.我必须先更新我的 contentPane。 Then the vertical scrollbar got updated to the correct values.然后垂直滚动条更新为正确的值。 Idk why it worked with the horizontal scrollbar... Idk 为什么它与水平滚动条一起使用......

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.我的JScrollPane每次都会新建,此时VerticalScrollBar的最大值设置为 100,因此它不能接受更高的值。

I fixed it by saving the maximum before constructing the new JScrollPane and then set it to the new Object.我通过在构造新的JScrollPane之前保存最大值来修复它,然后将其设置为新的对象。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM