aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JScrollPane resizing component in Viewport Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JScrollPane resizing component in Viewport" Watch "JScrollPane resizing component in Viewport" New topic
Author

JScrollPane resizing component in Viewport

Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hi there

I am using a JScrollPane to show a picture that I can zoom into and zoom out of using separate buttons.

When I zoom out of the image and make the image smaller than the Viewport, the image goes smaller for a second and then resizes to fit exactly into the Viewport.

I see in the API that


By default JScrollPane uses ScrollPaneLayout to handle the layout of its child Components. ScrollPaneLayout determines the size to make the viewport view in one of two ways:

If the view implements Scrollable a combination of getPreferredScrollableViewportSize, getScrollableTracksViewportWidth and getScrollableTracksViewportHeightis used, otherwise
getPreferredSize is used.


I had to make my own component to put the image on, and that has a getPreferredSize() method. And I don't know how to make the view implement Scrollable.

Does anyone have any suggestions to help fix this?

Cheers,
Rachel



(When the image is larger than the scroll pane, the behaviour is as expected and the scrollbars appear so that I can move to see the hidden parts of the image.)
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Sounds like you're on the right track. Return the desired Dimension from getPreferredSize and then ask the JScrollPane to update its view with a call to revalidate on the view (your custom) component.

In this demo I return the actual (scaled) image size. You could include padding in one or both dimensions if you like.
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Thanks Craig! That was exactly what I was looking for.

I think that the biggest problem with my version was that the code in getPreferredSize wasn't working as I expected it to.

I am so happy that this is finally resolved!

Thanks again!
Rachel

Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
I have read your code through and through but I can't see how you have managed to keep the component centered before the scrollbars come in. I have a feeling that it should be in the paintComponent but I don't see it. Which piece of code is controlling that?

Cheers,
Rachel
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
This code in paintComponent centers the image in the component

Set x and y equal to zero and see what happens.
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Excellent, looking at it now it seems so clear...

Thanks,
Rachel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JScrollPane resizing component in Viewport