5 Replies Latest reply: Nov 17, 2011 9:49 AM by 900772 RSS

    System tray dissappears after explorer crash

      On a windows system the tray icon associated with a java application dissapears after a crash of explorer.exe. Other applications seem to able to restore their system tray icon. Is there a way to restore the tray icon in a clean way? I currently just remove and add the tray icon to the task tray every 10 minutes. But this causes the tray to flash for a brieve moment.
        • 1. Re: System tray dissappears after explorer crash

          Recently, I noticed the same problem in one of my applications (windows XP). Your question triggered me to have a look at it again.

          A possible solution might be (not tested yet, but on my todo list) to not simply remove/add your TrayIcon on some arbitrary interval, but instead, check with some interval if the TrayIcon is still present in the SystemTray (getTrayIcons()). According to the api doc for add(TrayIcon), it will automatically be removed if the SystemTray becomes unavailable. Only when it has been removed, add it again.

          I think what happens as a result of certain crashes is that the SystemTray becomes temporarily unavailable.

          • 2. Re: System tray dissappears after explorer crash
            Yes that is something that I tried first. But it seems like the trayicon is still present if tested for it programmatically. There seem to be now way of detecting that the trayicon has visually dissapeared.
            • 3. Re: System tray dissappears after explorer crash
              That's a pity. Meanwhile I implemented it the way I suggested but didn't have the required crash yet. For me the short flashing is no problem, so I will revert to the way you did it. Sorry to be of no help.

              • 4. Re: System tray dissappears after explorer crash
                you can easily simulate a crash of explorer.exe by just killing it in the the task manager and then restarting it.

                Regarding the flashing of the task tray, things get worse when coming out of hibernate. It seems like when using ScheduledExecutorService.scheduleAtFixedRate all the executions that where not executed during hibernation are executed when coming out of hibernation. So because of the removal/add of the task tray icon the tray starts flashing like crazy untill all the missed executions are done.

                By the way I found a way of restoring the task tray without adding/removing the task tray icon by using reflections, but for some reasons this causes the application to become unstable after a certain while.
                  Field theField = TrayIcon.class.getDeclaredField( "peer" );
                  theField.setAccessible( true );
                  theField.set( myIcon, null ); 
                  Method theMethod = TrayIcon.class.getDeclaredMethod( "addNotify", new Class[]{} );
                  theMethod.setAccessible( true );
                  theMethod.invoke( myIcon, new Object[]{} );
                } catch ( Throwable e ) {
                   LOGGER.error( "Could not restore system tray icon", e );
                • 5. Re: System tray dissappears after explorer crash
                  I have tested this and it makes the icon coming back in the systray without any visible side effect (until now) such as blinking (if the icon was already there), etc.
                  For instance, I'm testing on XP SP3 with JRE1.6.0_24.

                  try {
                                      } catch (HeadlessException e) {
                                      } catch (AWTException e1) {

                  Hope that helps.
                  Best regards.

                  Edited by: user7539610 on 17 nov. 2011 06:26

                  I edit my post to confirm that I got also some issues with this trick : systay menu and window refresh problems...