3

Hi I'm able to add TrayIcon to the SystemTray and can remove the same form SystemTray.

How to remove an existing TrayIcon. I could not get the Object to available TrayIcons to do it.

Below is my code to add and remove TrayIcon

        TrayIcon trayIcon = new TrayIcon(icon);
        SystemTray tray = SystemTray.getSystemTray();
        PopupMenu popup = new PopupMenu();
        MenuItem exit = new MenuItem("Exit");         
        popup.add(exit);

        trayIcon.setToolTip("My TrayIcon");
        trayIcon.setPopupMenu(popup); 
        tray.add(trayIcon);

        trayIcon.displayMessage("Testing",
                "Information" ,
                TrayIcon.MessageType.INFO );
        tray.remove(trayIcon);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Hanumanth
  • 1,197
  • 2
  • 10
  • 14
  • Do you mean to remove one added by a different application? – Boann Feb 07 '14 at 09:31
  • Not exactly, I am adding a try icon when i start a test, if the test was stopped abruptly.. The icon is not getting unloaded. Next time if I run again one more icon will be added to the tray.Due to this I've a pile of same icons in system tray – Hanumanth Feb 07 '14 at 09:35
  • 1
    What do you mean "stopped abruptly"? Do you mean an exception is thrown? – Boann Feb 07 '14 at 09:36
  • If user closes the triggering batch file before completion of the Test – Hanumanth Feb 07 '14 at 09:38
  • 2
    If you mean that the process is forcibly terminated, then yes, it doesn't get a chance to remove its icon. This is a Windows bug. If you move the mouse over the leftover icon it suddenly disappears. – Boann Feb 07 '14 at 09:41
  • You could add a shoutdown hook to support that. But that is common behaviour on Windows OS. That all resources are nor released. – Damian Leszczyński - Vash Feb 07 '14 at 09:42
  • Thanks Boann for your reply. – Hanumanth Feb 07 '14 at 09:43
  • @Vash I don't think a shutdown hook would get a chance to run in this case. – Boann Feb 07 '14 at 09:45

3 Answers3

3
SystemTray tray = SystemTray.getSystemTray();
        try {
            boolean exist = false;
            for(TrayIcon icon : tray.getTrayIcons() ){
                if(icon.getImage().equals(trayIcon.getImage()) && icon.getToolTip().equals(trayIcon.getToolTip())){
                    exist = true;
                    break;
                }
            }
            if(exist){
                tray.remove(trayIcon);
                                }
itro
  • 7,006
  • 27
  • 78
  • 121
2

This is the correct way to remove icon, like you did:

SystemTray tray = SystemTray.getSystemTray();
tray.remove(trayIcon);

But, if you running the application from Eclipse, and instead of dispose the program window, you just stopping the run like "hard stop", so the icon is kept there in the try icon until you hover with the mouse so it disappear.

The reason is - Windows doesn't monitor if your program is still running or not, you should take care to close your resources before stopping the application.

Try somehow to run method closeProgram() when you close the program.

public void closeProgram()
{
    tray.remove(trayIcon);
    System.exit(0);
}

In my case I have some JFrame window, so I added window listener to do it

addWindowListener(new WindowAdapter()
{
    public void windowClosing(WindowEvent e)
    {
        closeProgram();
    }
});

In case you running the program using batch file, I think you will don't have a control on it, maybe better to hide the batch console, and show simple JFrame that displays the log prints, but the benefit - it will have "X" button to close the program.

Adir Dayan
  • 1,308
  • 13
  • 21
1

You have to remove it form SystemTray.

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.

In "How to Use the System Tray", tutorial you will find the answer

  • Link-only answers are discouraged on SO due to the moving nature of pages on the internet and SO's intention that answers remain valid for a long time to come. If you must answer via link, at least past in enough content from that link to provide a minimal answer that the poster could use without going off site. Otherwise you end up with a very low quality answer. – mah Feb 10 '14 at 12:17
  • @mah, Thanks four your concern. But the quote contains enough information how to add/remove try icon from it. Also OP in question has it used as it should. The issue here is that the OP wanted to know how to remove the try icon after abruptly exit. – Damian Leszczyński - Vash Feb 10 '14 at 19:21