views:

492

answers:

1

Hi,

I am making an image loading component which consists of a JPanel containing a JScrollPane, which in turn contains another JPanel. What this component does is allows images to be dropped on top of it, after which point the image is loaded and the inner most JPanel is set to the size of the image dropped. This in turn causes the scroll bars to show up and the user can scroll the image. This all works fine. The problem comes in when i try to auto-shrink the image to the maximum visible area in the outer JPanel. In this case i do a uniform scale of the image to be less than or equal to the width and height of the outer JPanel. What happens now is that both the horizontal and vertical scroll bars show up indicating the the inner JPanel is bigger than the visible area (which should not be the case). I verified that the image is scale to the proper dimensions(ie. the maximum width and height is respected). I also verified that if i decrease the maximum height by 3 pixels, then no scroll bars appear.

What i believe the problem is, is that panel.getWidth() and panel.getHeight() don't actually return the visible area (maximum area) that sub components can take up. Ie. there is likely some more width and height taken up by the border around the JPanel or something like that.

My question is, how do i get around this problem. Functionally all i want is to determine the maximum size a JPanel can be in a JScrollPane, then set the panel to that size and paint an image over top of it and be assured that the scroll bars of the scroll pane will not show up. Right now the scroll bars are set to AS_NEEDED.

Here's a portion of the code:

JPanel. Insets insets = getInsets();
int width = getWidth() - insets.left - insets.right;
int height = getHeight() - insets.top - insets.bottom;
imageScaled = ImageUtils.uniformScaleImage(imageOriginal, width, height);

Thanks!

A: 

Functionally all i want is to determine the maximum size a JPanel can be in a JScrollPane

scrollPane.getViewport().getViewSize();
camickr
Thanks, but unfortunately, this gives me the size of the container inside the JScrollPane. What i want is the size of the viewing area of the scroll pane. Ie. I want a width and height that i can make the inner JPanel (inside the scroll pane) that will fit inside the scroll pane without making the scroll bars appear when set to AS_NEEDED. I hope that makes sense.
Coder
Then use viewport.getSize(). I thought the two where the same, but apparently not. Or maybe its the extent size. At any rate the answer you seek is contained within the viewport methods.
camickr
thanks i will try those
Coder
Ok that worked, thanks a lot. I had to make a minor addition as it seems getSize() is affected by the scroll bars being visible.The code is below.Cheers!Dimension vSize = imageScrollPane.getViewport().getSize(); int width = vSize.width; int height = vSize.height; if (vs.isVisible()) { width += vs.getWidth(); } if (hs.isVisible()) { height += hs.getHeight(); }where vs and hs are the scroll bars from the imageScrollPane
Coder