4

I've built an application that hides in the system tray via the SystemTray class. Works ok, but when I quit the program and start it again, it doesn't remove the icon from the tray before hovering it with the cursor. Doing this multiple times causes multiple icons to appear:

enter image description here

Is this a problem with my program or is it a Windows' bug?

I followed this guide to create the system tray icon.

MikkoP
  • 4,864
  • 16
  • 58
  • 106
  • 2
    How are you ensuring that remove is called on a shutdown? – Peter Lawrey Dec 30 '12 at 15:58
  • 1
    Just checking, but have you confirmed that the previous application instance has closed in task manager? – JayTee Dec 30 '12 at 15:59
  • I know a lot of programs which behave in that way when they crash instead of exiting normally. I agree with @PeterLawrey: You are likely lacking a systemTray.remove(icon) call. – Philipp Dec 30 '12 at 16:00
  • Ah, you have to call such function? Thanks for letting me know. But to call that I need to know when the user quits the application. Can I bind the close event? – MikkoP Dec 30 '12 at 16:03
  • +1 interesting. Make sure releasing icon from tray when shutdown – vels4j Dec 30 '12 at 16:04

2 Answers2

6

When an application which created a tray icon is terminated, Windows doesn't remove the tray icon automatically. It only does so when the user hovers over it with the mouse and notices that the owning process doesn't exist anymore.

To make sure that the icon is removed immediately, you have to call systemTray.remove(yourIcon) when your program quits.

Philipp
  • 67,764
  • 9
  • 118
  • 153
1

The system tray contains one or more tray icons which are added to the tray using the add(java.awt.TrayIcon) method. They can be removed when they are no longer needed with the remove(java.awt.TrayIcon) method.

vels4j
  • 11,208
  • 5
  • 38
  • 63