Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 10468
  • Last Modified:

JLabel Icon Resize

I have a JPanel in which I placed a JLabel of the same size as the JPanel.  I set the icon property of the JLabel to a jpg image file.  The image file displays, but it is too large, and needs to be resized.

How do I resize the displayed (in the JLabel) icon to be the same height and width as the JLabel?

Possibly, JLabel icons cannot be resized, which would explain why I'm running into a dead end.  If so, would someone tell me the best way to display a picture in a JPanel, sized to the same size as the JPanel?

(As an aside, my IDE is Netbeans.  But, I think this is just a Java question.)
0
LarryAndro
Asked:
LarryAndro
  • 3
  • 2
2 Solutions
 
UrosVidojevicCommented:
One possible solution is to resize .jpg image with some external editor (for example Paint), and use resized image from Netbeans.
0
 
LarryAndroAuthor Commented:
Thank you for suggestion to resize the actual image.  I can do that with a batch image editor like IrfanView.  But, since I use the image for several reasons, I would prefer to programmatically resize the image at the time of use.

Hopefully, there is a way...
0
 
UrosVidojevicCommented:
Method for resizing ImageIcon objects:
(
Modified from source:
http://forum.java.sun.com/thread.jspa?threadID=789874&messageID=4488810
)

    public ImageIcon resizeImageIcon(ImageIcon imageIcon, double factor) {
        Image image = imageIcon.getImage();
       
        int width = (int) (factor * image.getWidth(null));
        int height = (int) (factor * image.getHeight(null));
       
        Image newImage  = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        ImageIcon newImageIcon = new ImageIcon(newImage);
       
        return newImageIcon;
    }

Use:

        ImageIcon icon = new ImageIcon("C:\\image.jpg");
        label.setIcon(resizeImageIcon(icon, 0.5));
0
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
objectsCommented:
this panel will resize the image according to the size of the panel.

http://www.objects.com.au/java/qa/1959359983.html
0
 
LarryAndroAuthor Commented:
I'm testing and researching these techniques.  I'll respond shortly.  Thanks for help...
0
 
LarryAndroAuthor Commented:
Success!  Great suggestions.  (Splitting points.)  And, thank you...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now