Search code examples
javadesktop-applicationsystem-traytrayicon

How to show icon file in system tray?


I am adding my application in system tray when I close it. But it is not displaying the icon. When I try to show picture file then it works fine but when I try with an icon file it does not work. How can I display an icon instead picture?

Image image = new ImageIcon("src/resources/busylogo.jpg").getImage();
final TrayIcon trayIcon = new TrayIcon(image);
try {
  SystemTray.getSystemTray().add(trayIcon);
} catch (AWTException e2) {
  e2.printStackTrace();
}

Solution

  • Better use Toolkit to load an icon. That's a low size file, and an asynchronous load will give you less problems. Try this code, which is recommanded by Sun.

    SystemTray tray = SystemTray.getSystemTray();
    Image image = Toolkit.getDefaultToolkit().getImage("src/resources/busylogo.jpg");
    final TrayIcon trayIcon = new TrayIcon(image);
    try {
       tray.add(trayIcon);
    } catch (AWTException e2) {
       e2.printStackTrace();
    }
    

    More informations here : http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/systemtray/