aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JScrollPane in JInternalframe and JPanel in JScrollPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JScrollPane in JInternalframe and JPanel in JScrollPane" Watch "JScrollPane in JInternalframe and JPanel in JScrollPane" New topic
Author

JScrollPane in JInternalframe and JPanel in JScrollPane

Marc Mueller
Greenhorn

Joined: Jan 26, 2012
Posts: 4
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

Joined: Jun 13, 2009
Posts: 2227
    
    8
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

Joined: Jan 26, 2012
Posts: 4
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

Joined: Oct 27, 2005
Posts: 19785
    
  20

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..


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Marc Mueller
Greenhorn

Joined: Jan 26, 2012
Posts: 4
Thank you very much! That works perfectly well. Could you also explain what happens in the background? Thank you!

Regards
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
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

Joined: Jan 26, 2012
Posts: 4
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: JScrollPane in JInternalframe and JPanel in JScrollPane