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 GIF image added to JPanel doesn't resize Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "GIF image added to JPanel doesn Watch "GIF image added to JPanel doesn New topic
Author

GIF image added to JPanel doesn't resize

Arun Bommannavar
Ranch Hand

Joined: Jan 11, 2003
Posts: 53
I can add a GIF (or PNG) to a JPanel (see code below) but the image doesn't rescale automatically. I need to have this image rescale when the JPanel is resized. I appreciate any suggestion.



Regards
Arun
Gregg Bolinger
Ranch Hand

Joined: Jul 11, 2001
Posts: 15229

Well, the problem is that you are adding a JLabel to a JPanel and chances are the JLabel isn't resizing. So its not really the images. If you are simply trying to add an image to a JPanel (as a background image) then you should do it something like what is written here.

If you still need it on a JLabel then you need to be sure your layout manager is resizing your JLabel correctly.
Arun Bommannavar
Ranch Hand

Joined: Jan 11, 2003
Posts: 53
Originally posted by Gregg Bolinger:
Well, the problem is that you are adding a JLabel to a JPanel and chances are the JLabel isn't resizing. So its not really the images. If you are simply trying to add an image to a JPanel (as a background image) then you should do it something like what is written here.

If you still need it on a JLabel then you need to be sure your layout manager is resizing your JLabel correctly.


I finally ended up overriding the paintComponent as follows:



works well.

Regards
Arun
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Please keep paintComponent protected. It's unnecessary to have it be public.

I already updated the FAQ about this.
[ August 01, 2008: Message edited by: Rob Prime ]

SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GIF image added to JPanel doesn't resize
 
Similar Threads
Scaling image: black box until repainting
Displaying multiple images
Icons
Image as a background
Refreshing JLabel