Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-06-2007, 03:47 PM
Member
 
Join Date: Dec 2007
Posts: 15
Soda is on a distinguished road
How to set an Icon in a Label?
I cant seem to put images in a label. I have tried a lot of codes and still cant get it.... Any tips?

Quote:
Here are some codes:

Code : lName.setIcon("./IMG/name_icon.jpg");

Error : setIcon(javax.swing.Icon) in javax.swing.JLabel cannot be applied to (java.lang.String)


Code : Image icon = createImageIcon("./IMG/name_icon.jpg");
lName = new JLabel(icon);

Error : cannot find symbol method createImageIcon(java.lang.String)
cannot find symbol constructor JLabel(java.awt.Image)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-06-2007, 09:07 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,191
hardwired is on a distinguished road
Using ImageIcon
Code:
Code : lName.setIcon("./IMG/name_icon.jpg"); Error : setIcon(javax.swing.Icon) in javax.swing.JLabel cannot be applied to (java.lang.String)
JLabel.setIcon method takes the interface type Icon which is implemented by the ImageIcon class. You find this by looking up the setIcon method in the JLabel class api. Follow the link (in the arguemnt) to the Icon interface, note that ImageIcon is an implementing class, follow the link to the ImageIcon api and check out the class constructors: there are 9 in my j2se 1.5 javadocs.
So try something like:
Code:
ImageIcon icon = new ImageIcon("IMG/name_icon.jpg"); System.out.println("icon = " + icon); // test for null lName.setIcon(icon); // or URL url = getClass().getResource("IMG/name_icon.jpg"); System.out.println("url = " + url); iName.setIcon(new ImageIcon(url)); // or lName.setIcon(new ImageIcon(new File("IMG/name_icon.jpg"));
Code:
Code : Image icon = createImageIcon("./IMG/name_icon.jpg"); lName = new JLabel(icon); Error : cannot find symbol method createImageIcon(java.lang.String) cannot find symbol constructor JLabel(java.awt.Image)
Check the argument type for the createImageIcon method you are calling. The compiler says it does not take a String argument.
Also, the type of "icon" is wrong according to the compiler. You have declared the "icon" variable as type Image. This type declaration must match the return type declaration of the createImageIcon method you are calling. From the name of the method I would suspect/infer that its return type is ImageIcon, ie, it may have a method signature like this:
Code:
public ImageIcon createImageIcon(args_of_some_type) { ImageIcon icon = new ImageIcon(some_args); ... return icon; } // Therefore: ImageIcon icon = createImageIcon(args);
ImageIcon is an older (j2se 1.2) way of loading images and does not report trouble. Its api and the underlying MediaTracker api provide methods you can use to ask about loading success. The newer way to load images (j2se 1.4+) is ImageIO.read methods. See api for options. It does report trouble.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-07-2007, 01:38 PM
Member
 
Join Date: Dec 2007
Posts: 15
Soda is on a distinguished road
Thnx much!!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting disk label Java Tip Java Tips 0 02-05-2008 10:07 AM
Example of SWT Label Java Tip Java Tips 0 01-09-2008 01:02 PM
Using java.awt.Label Java Tip Java Tips 0 01-02-2008 07:28 PM
SWT Label JavaForums Java Blogs 0 12-31-2007 05:53 PM
Label image Question Soda New To Java 3 12-10-2007 04:38 PM


All times are GMT +3. The time now is 06:55 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org