How do I save my JScrollPane position after I do a JFrame.pack()?

梦想与她 提交于 2019-12-10 21:46:43

问题


I have the following code:

JFrame frame = new JFrame();
JScrollPane scrollPane = new JScrollPane(new panel(with stuff in it));
frame.getContentPane().add(scrollPane);

So the user scrolls a bit and then clicks a link on my panel and then I do a pack(), so I try the following code:

int val = scrollPane.getVerticalScrollBar().getValue();
frame.pack();
scrollPane.getVerticalScrollBar().setValue(val);

But this code still returns my scrollBar back to the beginning instead of keeping the position it was originally at before the pack. Any ideas would be appreciated, Thanks!


回答1:


Try wrapping the setValue() method in a SwingUtilities.invokeLater().



来源:https://stackoverflow.com/questions/6335651/how-do-i-save-my-jscrollpane-position-after-i-do-a-jframe-pack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!