Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JScrollPane in JInternalframe and JPanel in JScrollPane

 
Marc Mueller
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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

 
Rob Camick
Ranch Hand
Posts: 2542
9
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Ranch Hand
Posts: 2542
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic