Results 1 to 3 of 3
Thread: How to set an Icon in a Label?
- 12-06-2007, 02:47 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 15
- Rep Power
- 0
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?
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)
- 12-06-2007, 08:07 PM #2
Using ImageIcon
Java Code:Code : lName.setIcon("./IMG/name_icon.jpg"); Error : setIcon(javax.swing.Icon) in javax.swing.JLabel cannot be applied to (java.lang.String)
So try something like:
Java 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"));
Java 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)
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:
Java Code:public ImageIcon createImageIcon(args_of_some_type) { ImageIcon icon = new ImageIcon(some_args); ... return icon; } // Therefore: ImageIcon icon = createImageIcon(args);
- 12-07-2007, 12:38 PM #3
Member
- Join Date
- Dec 2007
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
Getting disk label
By Java Tip in forum Java TipReplies: 0Last Post: 02-05-2008, 09:07 AM -
Example of SWT Label
By Java Tip in forum Java TipReplies: 0Last Post: 01-09-2008, 12:02 PM -
Using java.awt.Label
By Java Tip in forum Java TipReplies: 0Last Post: 01-02-2008, 06:28 PM -
Label image Question
By Soda in forum New To JavaReplies: 3Last Post: 12-10-2007, 03:38 PM
Bookmarks