I extended JPanel into something called a DrawingSurface that is used
to render images. I set up a JScrollPane with a DrawingSurface
instance as its client. When the drawing surface loads an image, it
changes its size by calling setSize(). To my chagrin, my JScrollPane
never worked right - the scrollbars never appeared at all.
This changed once I started setting my DrawingSurface's preferred size
in addition to its actual size. Suddenly, the scroll pane started
doing what I needed it to do.
Is setting a preferred size on a scroll pane's client a requirement
for it to work correctly? It seems like it from my chair, but I find
it odd that this doesn't seem to be mentioned in any of the tutorials.
I even googled the living daylights out of this problem, and still had
to noodle it out on my own.
ff
Paul Lutus - 27 Oct 2004 02:27 GMT
> I extended JPanel into something called a DrawingSurface that is used
> to render images. I set up a JScrollPane with a DrawingSurface
> instance as its client.
Using what method, in what code? Have you considered submitting the above
description to your Java compiler? Don't you think the compiler might have
a hard time sorting out what you actually intended? We have the exact same
problem.
> When the drawing surface loads an image, it
> changes its size by calling setSize(). To my chagrin, my JScrollPane
> never worked right - the scrollbars never appeared at all.
Maybe that is what you told it to do. In fact, that is almost certainly the
case.
> This changed once I started setting my DrawingSurface's preferred size
> in addition to its actual size. Suddenly, the scroll pane started
> doing what I needed it to do.
Which was what?
> Is setting a preferred size on a scroll pane's client a requirement
> for it to work correctly?
Define "correctly"? You never say what you wanted, what you got, and how
they differ.

Signature
Paul Lutus
http://www.arachnoid.com
Babu Kalakrishnan - 27 Oct 2004 06:39 GMT
> I extended JPanel into something called a DrawingSurface that is used
> to render images. I set up a JScrollPane with a DrawingSurface
[quoted text clipped - 11 lines]
> I even googled the living daylights out of this problem, and still had
> to noodle it out on my own.
This is something of a FAQ in this newsgroup. The setSize() (as well as
the setLocation / setBounds) method is meant to be called only by the
LayoutManager, and an application code call to it will work reliably
only if the component is not under the control of any LayoutManager.
(For example on your top level container or on a component placed in a
container that has a null LayoutManager).
In order to perform a Layout, all containers needs to know how big each
child needs to be. The JScrollPane is nothing different in this matter.
In case the child (in your case the drawing surface) cannot
_automatically_ determine how big it needs to be, you have to give it a
helping hand by setting its preferred size. Note that the images that
you're rendering on the panel are _not_ its children - they are only
painted on it. A panel can compute its size only based on what children
it contains - not based on painting code.
Also keep in mind that calling setPreferredSize should be avoided on
containers that _can_ compute their preferred sizes based on the
children they contain.
BK
Fritz Foetzl - 27 Oct 2004 17:21 GMT
>> Fritz Foetzl wrote:
>> [stuff]
> This is something of a FAQ in this newsgroup. The setSize() (as well as
> the setLocation / setBounds) method is meant to be called only by the
[quoted text clipped - 17 lines]
>
> BK
I hate to re-ask FAQs, because I know how annoying it can be. Before
posting, I looked and looked through this ng, but couldn't find the
answer (or didn't understand it when I looked at it).
Anyway, this suddenly makes sense, and I appreciate your taking the
time to explain it to me. In the process, you also answered an
as-yet-unspoken question I had about containers re-sizing themselves
based on their children.
Thank you very much!
ff