3

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!

Grammin
  • 11,808
  • 22
  • 80
  • 138

1 Answers1

3

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

camickr
  • 321,443
  • 19
  • 166
  • 288