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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

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.


GenRocket - Experts at Building Test Data
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: 19794
    
  20

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 - OCEEJBD 6
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