How to add an Icon to a Java application

This are the instructions to add an icon the a Java Swing application.

Icon is loaded from an image. In order to load this image we make use of File and Buffered Image classes.

Icon can be shown in different places, depending on operation system:

  1. On a corner of the window bar, like in Windows. In this example we are adding the icon to the bar of a Java Swing’s JFrame component.
  2. On the system tray, like in Mac OS X.

In this example we put the icon in all the identified places.

package appicon;
import java.awt.Taskbar;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class AppIcon {
public AppIcon() {
//create app frame
JFrame frame;
frame = new JFrame();
JPanel panel;
panel = new JPanel();
JLabel label;
label = new JLabel("This Java app has an icon.");
//read image
String filepath = "src/appicon/image.png";
File file = new File(filepath);
BufferedImage bImage;
try {
bImage =;
//set icon on JFrame menu bar, as in Windows system
//set icon on system tray, as in Mac OS X system
final Taskbar taskbar = Taskbar.getTaskbar();
} catch (IOException ex) {
Logger.getLogger(AppIcon.class.getName()).log(Level.SEVERE, null, ex);
//make frame visible
public static void main(String[] args) {
new AppIcon();

Leave a Reply

Your email address will not be published. Required fields are marked *