The javax.swing package contains the ImageIcon class, which extends the Object class and provides the Serialisable and Icon interfaces. It is intended to show icons derived from images, and it supports MediaTracker for preloading these images.
The class facilitates the creation of icons from file paths or URLs, making sure that the picture loads correctly before being shown. It is crucial to remember that there is no assurance that the serialized objects of this class will work with releases of Swing later than Swing 1.0.
The class is compatible with Swing 1.0 and supports temporary serialization; however, compatibility may be broken in future versions.
You can represent an image that is readily usable in Swing application components with the help of the ImageIcon class. The image can be shown in GUI components by loading it from the file system, a URL, or any other source.
Several constructors can be used to produce an ImageIcon, including:
| Constructor | Description |
|---|---|
| ImageIcon() | Creates an uninitialized image icon with no image set. |
| ImageIcon(byte[] imageData, String description) | Creates an ImageIcon from a byte array of image data and a description. This byte array is usually loaded from a supported image format like GIF or JPEG. |
| ImageIcon(byte[] imageData) | Similar to the previous constructor but without a description. It creates the icon from the byte array. |
| ImageIcon(Image image, String description) | Creates an ImageIcon from an Image object with a description. |
| ImageIcon(Image image) | Creates an ImageIcon from an Image object without a description. |
| ImageIcon(String filename, String description)Creates an ImageIcon from a file specified by the file path, with an optional description. | |
| ImageIcon(String filename) | Similar to the previous constructor but without a description. |
| ImageIcon(URL location, String description) | Creates an ImageIcon from a URL with a description. Uses MediaTracker to monitor the image loading state. |
| ImageIcon(URL location) | Similar to the previous constructor but without a description. |
| Method | Description |
|---|---|
| String getDescription() | Returns the description of the image. |
| int getIconHeight() | Returns the height of the icon. |
| int getIconWidth() | Returns the width of the icon. |
| Image getImage() | Retrieves the Image associated with the ImageIcon. |
| int getImageLoadStatus() | Returns the status of the image loading operation. |
| ImageObserver getImageObserver() | Returns the ImageObserver for the image. |
| void loadImage(Image image) | A protected method that waits for the image to load fully. |
The ImageIcon class in Java Swing is used to create icons from images. The following examples demonstrate how to create an image icon, display it in a label, and add it to a button.
In the following example, we create an ImageIcon object from an image file and display its description.
Java
import javax.swing.ImageIcon;
public class ImageIconExample1 {
public static void main(String[] args) {
ImageIcon icon = new ImageIcon("logo.png");
System.out.println("Image Icon created.");
System.out.println("Icon width: " + icon.getIconWidth());
System.out.println("Icon height: " + icon.getIconHeight());
}
}
Output:
Image Icon created. Icon width: 200 Icon height: 100
Note: The output dimensions assume that logo.png exists and has dimensions of 200 × 100 pixels. The actual dimensions depend on the image file.
In the following example, we create an ImageIcon object and display it inside a JLabel in a Swing window.
Java
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class ImageIconExample2 {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("ImageIcon Example");
ImageIcon icon = new ImageIcon("logo.png");
JLabel label = new JLabel(icon);
frame.add(label);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
Output:
A window titled ImageIcon Example appears and displays logo.png inside a label. The image file must exist at the specified path.
In the following example, we create a button with an image icon using the JButton class.
Java
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class ImageIconExample3 {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("ImageIcon Button Example");
ImageIcon icon = new ImageIcon("logo.png");
JButton button = new JButton("Click Me", icon);
button.addActionListener(e ->
System.out.println("Button clicked.")
);
frame.add(button);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
Output:
A window titled ImageIcon Button Example appears with a button labeled Click Me and displaying the image icon. When the button is clicked, the following message appears in the console:
Button clicked.
Note: Place logo.png in the application's working directory or replace the filename with the correct image path. ImageIcon does not automatically report an error when an image cannot be loaded, so verify the file path if the icon is missing.
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India