The Source for Java Technology Collaboration
User: Password:



Michael Nascimento Santos

Michael Nascimento Santos's Blog

Displaying messages in the system tray

Posted by mister__m on February 15, 2007 at 03:56 AM | Comments (8)

Many folks are aware that Java SE 6 comes with the new SystemTray and TrayIcon classes that allows an icon to be added to the system tray - or status area or whatever it is called on your platform. One very cool feature that most articles/pieces don't mention is it is possible to display a message similar to the "Low battery" warning produced by laptops or the "Updates are ready to be installed" alert Windows Update displays so often.

Here is the code:

public class SysTray {
   public static void main(String[] args) throws Exception {
      TrayIcon icon = new TrayIcon(getImage(), "Java application as a tray icon", 
            createPopupMenu());
      icon.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "Hey, you activated me!");
         }
      });
      SystemTray.getSystemTray().add(icon);

      Thread.sleep(3000);

      icon.displayMessage("Attention", "Please click here", 
            TrayIcon.MessageType.WARNING);
   }

   private static Image getImage() throws HeadlessException {
      Icon defaultIcon = MetalIconFactory.getTreeHardDriveIcon();
      Image img = new BufferedImage(defaultIcon.getIconWidth(), 
            defaultIcon.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR);
      defaultIcon.paintIcon(new Panel(), img.getGraphics(), 0, 0);

      return img;
   }

   private static PopupMenu createPopupMenu() throws HeadlessException {
      PopupMenu menu = new PopupMenu();

      MenuItem exit = new MenuItem("Exit");
      exit.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            System.exit(0);
         }
      });
      menu.add(exit);

      return menu;
   }
}

When you run this sample, an hard-drive icon (borrowed from Swing's Metal L&F) will be displayed in the system tray. If you place the mouse over it for a few seconds, the "Java application as a tray icon" tooltip will be shown and right-clicking it will show a popup menu with a single option, Exit, that will terminate our sample.

The cool thing is that approximately 3 seconds after the icon appears, a warning message will pop up in your system tray - and if you are running Windows and hide the task bar, it will even become immediately visible. This feature can be quite useful for some scenario, such as monitoring applications.

There are several other hidden gems in Java SE 6. Let's keep on searching for them...


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • nice

    Posted by: nemesis666 on February 15, 2007 at 05:18 AM

  • It's a pity the design of the system tray support was so XP-centric... The Mac version goes up into the top-right of the system menu area, which is fine for menu-type accesses, but for notifications, you'd normally do things like the bounce a dock icon, or place a badge on it.

    I've sure this is going to look quite dated with Vista too.

    Posted by: goron on February 15, 2007 at 08:19 AM

  • these messages are not shown in Linux :-(

    Posted by: francisdb on February 15, 2007 at 04:14 PM

  • thanks for the great tip and sample code :)

    Posted by: evanx on February 16, 2007 at 08:37 AM

  • Thanks, it looks nice for me, under Gnome/Linux into the notification area.

    I guess it will be implemented the good way by Apple, for Mac OS X.

    Posted by: arsene on February 16, 2007 at 10:00 AM

  • One of the reasons I was looking forward to 1.6 was the TrayIcon under linux. Please notice, `was looking forward to`. It is just not working. I'm using KDE, not GNOME. I like to configure my desktop the way I want it to look and feel. And 1.6's tray icon does not work :(. The funny thing is the JDIC examples (including the tray icon) works fine. Maybe if I go back to GNOME version 0.0.1 than sun's code wil work. Good thing they've opensourced it, because they for some odd reason are not willing to support KDE. Now GNOME may be easer to code to, but if SUN had the savvy to port swing to M$-windows, then I see no reason why it the system tray cannot work under KDE. I know, I use to program windows, and there are few things more horrific than working directly with the windows API!

    Posted by: wildfire on February 18, 2007 at 11:18 PM

  • TrayIcon does not work on KDE because of Sun's bug #6448876. Those guys claims it is fixed in Java7.

    Posted by: ghoul on February 21, 2007 at 12:16 AM

  • We've released an Alert application in my previous company that uses SystemTray/TrayIcon features, JMS for messaging management and WEB UI for administration. It works on Win and Linux machines for about 40 users (for now)... Very nice features.

    Posted by: realsoft on February 21, 2007 at 01:13 AM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds