1

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.

SharonBL
  • 1,735
  • 5
  • 20
  • 28
  • See also http://stackoverflow.com/questions/7923645/how-to-re-add-icon-to-system-tray-after-explorer-exe-crash/7923753 – 9000 Dec 01 '11 at 19:53
  • I saw this thread before. Do you know how to do it? – SharonBL Dec 01 '11 at 23:42
  • Alas, I don't have a Windows machine around to try it. I linked a post in the comments showing how to do it using [JNA](https://github.com/twall/jna). Maybe it works. Also, I can't tell whether this library is an overkill for your program. As a quick fix, you can just re-register your icon every minute or so — _eventually_ it will show up again. – 9000 Dec 01 '11 at 23:52

1 Answers1

0

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);
        }
   }
}
Dmitry B.
  • 9,107
  • 3
  • 43
  • 64
  • 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. – SharonBL Dec 01 '11 at 12:46