JLabel Icon Resize

Posted on 2007-11-19
Last Modified: 2013-11-23
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.)
Question by:LarryAndro
  • 3
  • 2

Expert Comment

ID: 20316084
One possible solution is to resize .jpg image with some external editor (for example Paint), and use resized image from Netbeans.

Author Comment

ID: 20316100
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...

Accepted Solution

UrosVidojevic earned 250 total points
ID: 20316265
Method for resizing ImageIcon objects:
Modified from source:

    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;


        ImageIcon icon = new ImageIcon("C:\\image.jpg");
        label.setIcon(resizeImageIcon(icon, 0.5));
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

LVL 92

Assisted Solution

objects earned 250 total points
ID: 20317833
this panel will resize the image according to the size of the panel.

Author Comment

ID: 20319513
I'm testing and researching these techniques.  I'll respond shortly.  Thanks for help...

Author Closing Comment

ID: 31410030
Success!  Great suggestions.  (Splitting points.)  And, thank you...

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 121
difference of if loops 23 62
expectj telnet failing 5 45
Java program running SQL query 5 38
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Many programs have tried to outwit PowerPoint in terms of technology and skill. These programs, however, still lack several characteristics that PowerPoint has possessed from the start. Here's why PowerPoint replacements won't entirely work for desi…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Learn the basics of inputting and editing your text components in Prezi. We will cover how to set styles, position, and group your text components. In your Prezi editor, click anywhere on the canvas to add text: A flashing cursor informs you that yo…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question