Solved

Prevent imageicon inside a JLabel from stretching to fit the JLabel

Posted on 2006-06-19
15
1,163 Views
Last Modified: 2008-01-09
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
Comment
Question by:Priestex
  • 4
  • 3
  • 3
  • +2
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16932931
Try setting the layout of the label to FLowLayout
0
 

Author Comment

by:Priestex
ID: 16932958
No, setting the layout to flowlayout does not help.

Can you think of something else?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16932966
>>No, setting the layout to flowlayout does not help.

Did you set it *before* setting the icon?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16932970
(IOW you can't use the ctor to set the icon)
0
 

Author Comment

by:Priestex
ID: 16933003
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16933027
Try setting the layout to null then
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 2

Expert Comment

by:tbeernot
ID: 16933041
How about using a border to increase the area the JLabel covers?

label.setBorder(BorderFactory.createEmptyBorder(10,20,30,40));
0
 

Author Comment

by:Priestex
ID: 16933068
Yeah i already tried the empty border thing, but it doesnt seem to be working .
0
 
LVL 2

Accepted Solution

by:
tbeernot earned 25 total points
ID: 16933165
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
 
LVL 30

Expert Comment

by:mayankeagle
ID: 16933329
>> 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
 
LVL 92

Assisted Solution

by:objects
objects earned 25 total points
ID: 16938384
you can't use a JLabel for that. Instead subclass JPanel and handle painting the image yourself.
0
 
LVL 2

Expert Comment

by:tbeernot
ID: 17121763
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 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

20 Experts available now in Live!

Get 1:1 Help Now