Improve company productivity with a Business Account.Sign Up

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

Prevent imageicon inside a JLabel from stretching to fit the JLabel

Hi,

I have  a JLabel and I have set an imageicon in it through the JLabel constructor.
Then I set the size of the JLabel with setBounds().
The setbounds() method sets the size of the JLabel that is larger than the size
of the image that i have set in the label as content.

My problem is that the imageIcon that I set in the JLabel, stretches to fit
the JLabel.

Is there any way of keeping the ImageIcon from stretching to fit the label?

I want the iamge icon to remian its original propotion and size and the label
to show its normal background for the rest of the area that the imageicon doesnt cover.

Thanx
0
Priestex
Asked:
Priestex
  • 4
  • 3
  • 3
  • +2
2 Solutions
 
CEHJCommented:
Try setting the layout of the label to FLowLayout
0
 
PriestexAuthor Commented:
No, setting the layout to flowlayout does not help.

Can you think of something else?
0
 
CEHJCommented:
>>No, setting the layout to flowlayout does not help.

Did you set it *before* setting the icon?
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
CEHJCommented:
(IOW you can't use the ctor to set the icon)
0
 
PriestexAuthor Commented:
Yes, i set the layout before setting the icon.

Here's the code:

//Icon is my image icon.
JLabel label = new JLabel();
label.setLayout(new FlowLayout());
label.setIcon(icon);
label.setBounds(x, y, width, height);

This doesnt work.
The icon still stretches to cover the label.

0
 
CEHJCommented:
Try setting the layout to null then
0
 
tbeernotCommented:
How about using a border to increase the area the JLabel covers?

label.setBorder(BorderFactory.createEmptyBorder(10,20,30,40));
0
 
PriestexAuthor Commented:
Yeah i already tried the empty border thing, but it doesnt seem to be working .
0
 
tbeernotCommented:
The code below generates a frame with the icon somewhere on the upper left side, but with different offsets to each side of the frame (you need to change the icon file). So the JLabel covers a largere area that the icon alone does without stretching.

      public static void main(String[] args)
      {
            JLabel lJLabel = new JLabel(new ImageIcon(Test.class.getResource("icon_action.gif")));
            lJLabel.setBorder( BorderFactory.createEmptyBorder(10,20,30,40));

            JFrame lJFrame = new JFrame();
            lJFrame.getContentPane().add(lJLabel);
            lJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            lJFrame.pack();
            lJFrame.setVisible(true);
      }
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> Then I set the size of the JLabel with setBounds

AFAIK setBounds () can only be used in the null layout. Did you try setPreferredSize ()?
0
 
objectsCommented:
you can't use a JLabel for that. Instead subclass JPanel and handle painting the image yourself.
0
 
tbeernotCommented:
The core question is: "Is there any way of keeping the ImageIcon from stretching to fit the label?".

I think both objects and I provided a valid answer. I'm still curcious why my first empty border suggestion appearantly did not work, since my second example clearly shows it does. (I'm suspecting a different cause than the JLabel.)
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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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