0

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();
}
Mat
  • 202,337
  • 40
  • 393
  • 406
Vinay
  • 347
  • 1
  • 7
  • 16

1 Answers1

1

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/

zessx
  • 68,042
  • 28
  • 135
  • 158
  • @Vinay just seen you last comment : not sure you can load .ico files. Didn't found for TrayIcon, but ImageIcon (for example) only accepts JGP/PNG/GIF (http://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html) – zessx Mar 15 '12 at 09:49
  • thanx. its working fine for .jpg but not for .ico and what i want is .ico – Vinay Mar 15 '12 at 09:50
  • Confirmation, seems TrayIcon doesn't support ICO and BMP files. I guess it's because ICO is a Windows file format, so this can't be multiplatform (which is the Java main goal) – zessx Mar 15 '12 at 09:57
  • You can use this library to read Microsoft ICO file http://www.vdburgh.net/2/f/files/ICOReader/ – ecle Mar 15 '12 at 12:20