Swing / JScrollPane / setPrefferedSize question...

Hey all.

I’ve basically got code like this:

JFrame has a JScrollPane in it which contains a JPanel. All this is created, the JFrame is shown, user can do whatever they want. User chooses a menu item that loads in a background image to the JPanel. The JPanel does the code to load in the image, then it says setPreferredSize(image.width, image.height). Unfortunately, the JScrollBar refuses to recognize the difference change until I resize the window. Then once the window is resized, scroll bars appear within the JScrollPane.

Does anyone know of some sort of update method I can call on the JScrollBar or the JPanel (or anything, really) that will cause the JScrollBar to recognize that the preferred size of the JPanel changed?

Thanks.

Try calling revalidate() on the JScrollPane.

All JComponents have that method (from memory…)

It might help you to see it in a larger context. This classic example helps:

How to Use Scroll Panes

Revalidate did the job. Weird, because I thought I had tried that before. Thanks guys.