执行JFrame.pack()后如何保存JScrollPane位置? [英] How do I save my JScrollPane position after I do a JFrame.pack()?

查看:64
本文介绍了执行JFrame.pack()后如何保存JScrollPane位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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

因此,用户滚动一下,然后单击面板上的链接,然后执行pack(),因此尝试以下代码:

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

但是此代码仍将我的scrollBar返回到开头,而不是保持它原来在包装之前的位置.任何想法将不胜感激,谢谢!

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!

推荐答案

尝试将setValue()方法包装在SwingUtilities.invokeLater()中.

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

这篇关于执行JFrame.pack()后如何保存JScrollPane位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆