Restoring Java systray icon after explorer crashes

I saw a lot of question regarding this issue, but none of them had solution. My problem is: I wrote a small java app that has a systray icon. When windows explorer crashes (can be simulated with stopping explorer.exe process), after it is restored my systray icon disappears but my app process keeps on running.

Is there a way to restore my icon when explorer.exe reloaded?

My problem is that i don't have a window i go to. My systray itself is a launcher for some commands. I need something that listen to recreation of windows taskbar and then i can re-add it.

Answers


I would recommend re-registering the tray icon in a window activation event. This way the tray icon will be updated whenever you switch back to your app. For example:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyApp extends JFrame
{
   private static TrayIcon trayIcon = null;

   public static void main(String[] args) {
      JFrame app = new MyApp();
      app.setSize(400,400);
      app.setVisible(true);

      final Image image = Toolkit.getDefaultToolkit().getImage("MyImage.gif");

      app.addWindowListener( new WindowAdapter() {
         public void windowActivated(WindowEvent ev) {
            registerTrayIcon(image);
         }
      });
   }

   private static void registerTrayIcon(Image image) {
        SystemTray tray = SystemTray.getSystemTray();

        if( trayIcon != null ) {
           tray.remove(trayIcon);
        }

        trayIcon = new TrayIcon(image, "Tray Demo", null);

        try {
           tray.add(trayIcon);
        }
        catch(Exception ex) {
           throw new RuntimeException(ex);
        }
   }
}

Need Your Help

Android Java sharedPrefences logic isssue

java android

Android Java sharedPrefences logic isssue

question about throwing an object

c++

#include <iostream>

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.