MikkoP
MikkoP

Reputation: 5092

Java program appearing multiple times in system tray

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.

Upvotes: 4

Views: 1533

Answers (2)

vels4j
vels4j

Reputation: 11298

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.

Upvotes: 1

Philipp
Philipp

Reputation: 69663

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.

Upvotes: 6

Related Questions