File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes New Image 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 "New Image in JScrollPane" Watch "New Image in JScrollPane" New topic
Author

New Image in JScrollPane

Skantha Kandiah
Greenhorn

Joined: Jan 19, 2001
Posts: 4
Hi,
I am trying to create an application that can display different images in a single frame. The main frame contains a JScrollPane that contains a JPanel. Initally there is no image on the JPanel and there are no scrollbars on the scroll pane. When I add a large image to the JPanel the scrollbars don't appear on the scroll pane. What function do I need to call for the scroll bars to appear? I tried revalidate() and it did not work.
Thanks,
SK
Ashwin Desai
Ranch Hand

Joined: Jul 17, 2000
Posts: 124
Hi,
You add the scrollpane around the JPanel using
JScrollPane scroll = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
Or if you need the scrollbars to be present always, change the 2nd and 3rd parameters to
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS and
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS
Hope this helps.
Ashwin.
ming fan
Greenhorn

Joined: Nov 15, 2000
Posts: 21
I am not sure how you add this image to the JPanel, but I am supposing that you just paint this image on top of this panel. One way I think you can try is to set the preferred size and minimum size dynamically according to the image size you loaded, then call revalidate, and that should force the JScrollPane to scroll.
code sample:
public Dimension getMinimumSize()
{
return new Dimension(image.getWidth(panel), image.getHeight(panel));
}
in your load function, then call revalidate().
Hope this helps.

Originally posted by Skantha Kandiah:
Hi,
I am trying to create an application that can display different images in a single frame. The main frame contains a JScrollPane that contains a JPanel. Initally there is no image on the JPanel and there are no scrollbars on the scroll pane. When I add a large image to the JPanel the scrollbars don't appear on the scroll pane. What function do I need to call for the scroll bars to appear? I tried revalidate() and it did not work.
Thanks,
SK

Skantha Kandiah
Greenhorn

Joined: Jan 19, 2001
Posts: 4
Thanks for the help.
Adding the getPreferredSize() method and calling the revalidate() function fixed my problem. I had to use MediaTracker to wait for the image to finish loading before calling repaint() and revalidate().
But this seems like an awful lot of work just to display an image on a JPanel. I thought some of the work will be handled automatically, like revalidation when a component changes.
Isn't there a simple way to add and remove images from a panel?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: New Image in JScrollPane