• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

JScrollPane resizing component in Viewport

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent, looking at it now it seems so clear...

Thanks,
Rachel
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic