• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JScrollPane in JInternalframe and JPanel in JScrollPane

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a class (lets call it MyClass) which extends JPanel. In this class I execute the following method:



Note, that MyClass is only a panel on which I show a "Image" and draw some other primitives on top of it.

Unfortunately the Scrollpane does not show any bars on its sides. Do you have any Ideas why this could be?

Thank you for your help.

Regards scr

 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are doing custom painting on your panel then you need to implement the getPreferredSize() to return the dimension of your painting. Then the scrollpane will know if scrollbars are required or not.
 
Marc Mueller
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob!

Thank you for your reply.
Well actually my painting should always have the dimensions of the loaded Image.
How can I make the scrollpane know the needed size? And how should I implement the getPreferredSize() method?

Thanks and regards,
scr
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you override that method inside your JPanel then the JScrollPane will automatically add scroll bars if its view port becomes smaller than that size..
 
Marc Mueller
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much! That works perfectly well. Could you also explain what happens in the background? Thank you!

Regards
 
Rob Camick
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Well actually my painting should always have the dimensions of the loaded Image.



Then use a JLabel to display the image instead of a JPanel and it will calculate the preferred size for you.
 
Marc Mueller
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, thank you for your help so far.

There is another question I would like to ask in this context: Basically I have a JPanel inside a JScrollPane with is inside a JInternalFrame now. By following your advice of adding the "getPreferredSize()" method to the JPanel class I managed to make the scrollPane bars visible. However, in the meanwhile I added a zooming function which basically draws my loaded image according to a certain scale factor on the JPanel. The image can be resized by mouse events. This works fine, however the dimension of the bars of the JScrollPane do not adjust accordingly. They adjust if I resize the JInternalFrame. But not directly after my mouse action to resize the image.

How can I make the bars adjust directly after my repaint?

Edit: I already tried to call scrollPane.revalidate(); but it only seems to help partially: At the beginning only the vertical scrollbar is visible, since the image is smaller than the viewport in horizontal direction. When I zoom the image, nothing happens as long as the image width is smaller than the width of the viewport. As soon as the image width gets larger than the viewport width, the horizontal scrollbar appears and the vertical also gets updated to is supposed "size". If I continue zooming after that, both scrollbars do not change any more. (Only if I adjust the frame size, the scrollbars do also update according to the size of the JPanel on which my resized image is drawn).


Thank you & regards,
Marc
 
I found a beautiful pie. And a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic