Solved

JLabel Icon Resize

Posted on 2007-11-19
6
10,121 Views
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.)
0
Comment
Question by:LarryAndro
  • 3
  • 2
6 Comments
 
LVL 7

Expert Comment

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

Author Comment

by:LarryAndro
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...
0
 
LVL 7

Accepted Solution

by:
UrosVidojevic earned 250 total points
ID: 20316265
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Assisted Solution

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

http://www.objects.com.au/java/qa/1959359983.html
0
 
LVL 1

Author Comment

by:LarryAndro
ID: 20319513
I'm testing and researching these techniques.  I'll respond shortly.  Thanks for help...
0
 
LVL 1

Author Closing Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python Assistance 7 73
Java passing arguments (type error) 2 48
Why does the Android Studio logcat show this error when the user presses the button? 2 42
Syntax Error 2 46
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Not only does Prezi allow you to create non-linear presentations, it also makes it easy to apply transition animations between your frames. Learn how to apply a fade-in transition to select items and entire frames in this tutorial Select "Edit Path"…
In addition to being a great web-based presentation tool, Prezi also makes it easy to save your presentation as a PDF to share with others as well. Learn how in this tutorial. Select the share icon from the top menu in your Prezi editor: Select "D…

920 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now