How to Set Icon to a Jlabel from an Image from a Folder

How to set Icon to a JLabel from an image from a folder?

This is my directory structure :

                                packageexample
|
-------------------------------------------
| | |
build(folder) src(folder) manifest.txt
| |
swing(package) ComboExample.java
|
imagetest(subpackage)
|
ComboExample.class + related .class files

This is the content of the ComboExample.java file :

package swing.imagetest;    

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
    
public class ComboExample {

private String[] data = new String[]{
"geek0",
"geek1",
"geek2",
"geek3",
"geek4"
};
private String MESSAGE = "No Image to display yet...";
private JLabel imageLabel;
private JComboBox cBox;
private ActionListener comboActions = 
new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
JComboBox combo = (JComboBox) ae.getSource();
ImageIcon image = new ImageIcon(
getClass().getResource(
"/" + combo.getSelectedItem() + ".gif"));
if (image != null) {
imageLabel.setIcon(image);
imageLabel.setText("");
} else {
imageLabel.setText(MESSAGE);
}
}    
};

private void displayGUI() {
JFrame frame = new JFrame("Combo Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

JPanel contentPane = new JPanel();
imageLabel = new JLabel(MESSAGE, JLabel.CENTER);
cBox = new JComboBox(data);
cBox.addActionListener(comboActions);

contentPane.add(imageLabel);
contentPane.add(cBox);

frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String... args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new ComboExample().displayGUI();
}
});
}
}

NOW THE COMPILATION :

To compile I did this :

Gagandeep Bali@LAPTOP ~/c/Mine/JAVA/J2SE/src/packageexample
$ javac -d build src/*.java

Contents of Manifest File :

Sample Image

JAR File creation :

Gagandeep Bali@LAPTOP ~/c/Mine/JAVA/J2SE/src/packageexample
$ cd build

Gagandeep Bali@LAPTOP ~/c/Mine/JAVA/J2SE/src/packageexample/build
$ jar -cfm imagecombo.jar ../manifest.txt *

Now take this JAR File to any location having these images (geek0.gif, geek1.gif, geek2.gif, geek3.gif and geek4.gif), and run the JAR File, and see the results :-)

How to set image on jlabel from project folder?

If you want the images included within the Jar, then you will need to move the images folder into the src directory.

This will then require you to use something more like...

jLabel4 = new JLabel(new ImageIcon(getClass().getResource("/images/Logo1.png")));

To the load image.

If you want the images to remain external to your program Jars (and remain open to the file system), then you need to ensure that the execution context for the program is within the context of the project folder (at the same level as the src and images directory).

This can be done via the Project Properties -> Run properties. By default, when run, the program will execute from the context of the running projects folder.

Properties

If you're wanting to use external resources you can do two things to check for them...

One, create a File using the same path as the external resource you want to load and check to see if it exists...

 if (new File("images/Logo1.png").exists()) {...

Or if you can't seem to get it to work, check your current running context with...

System.out.println(new File(".").getCanonicalPath());

Which will tell your current working directory (beware this will throw an IOException).

You can also use the system property user.dir

System.out.println(System.getProperty("user.dir"));

Java: how to add image to Jlabel?

You have to supply to the JLabel an Icon implementation (i.e ImageIcon). You can do it trough the setIcon method, as in your question, or through the JLabel constructor:

Image image=GenerateImage.toImage(true);  //this generates an image file
ImageIcon icon = new ImageIcon(image);
JLabel thumb = new JLabel();
thumb.setIcon(icon);

I recommend you to read the Javadoc for JLabel, Icon, and ImageIcon. Also, you can check the How to Use Labels Tutorial, for more information.

Java adding ImageIcon to JLabel

Your problem lies here:

   ImageIcon image = new ImageIcon("C:/Documents and Settings/user/Desktop/hi/xD/JavaApplication2/image.png");
JLabel imagelabel = new JLabel(character);

You create an ImageIcon "image" but you create your JLabel with "character".

It should be:

JLabel imagelabel = new JLabel(image);

Adding Image from Filesystem to Jlabel

Suppose the images are stored in a folder say 'image' or whatever location. I am supposing that you have an image folder inside src (source folder), though you may change it to whatever location you want.

/*
URL logoUrl = this.getClass().getResource("/images/login_icon.png"); // you can change this location
Toolkit tk1 = this.getToolkit();
logo = tk1.getImage(logoUrl);
*/
// use above code if the image lies within your jar file
// otherwise use below code for images stored in path like C:\User\Desktop ..

ImageIcon image = new ImageIcon("C:\\Users\\Public\\Pictures\\Desert.jpg");
jLabel2.setIcon(image); // where jLabel2 is your label

This will work!

Set icon to JLabel - not display icon

It seems an issue with path of Check.png. Correct the path and icon shall appear.
If icon is stored under resource folder icons then path should be like below.

ImageIcon image = new ImageIcon("icons/Check.png");


Related Topics



Leave a reply



Submit