Have a systray icon (Windows)Tag(s): Swing


JDK 1.6 provides support for the Systray on the Windows plateform.

A small icon is located at the bottom right of the Desktop.

you can click on it to trigger an action or use this area to display notification.

Use this small image to run this demo :

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

public class SysTrayDemo {
    protected static TrayIcon trayIcon;
    private static PopupMenu createTrayMenu() {
        ActionListener exitListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("Bye from the tray");
                System.exit(0);
            }
        };

        ActionListener executeListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog
                   (null, "Popup from the action on the systray!",
                    "User action", JOptionPane.INFORMATION_MESSAGE);
                trayIcon.displayMessage
                   ("Done", "You can do it again if you want!",
                    TrayIcon.MessageType.INFO);
            }
        };

        PopupMenu menu = new PopupMenu();
        MenuItem execItem = new MenuItem("Action...");
        execItem.addActionListener(executeListener);
        menu.add(execItem);

        MenuItem exitItem = new MenuItem("Exit");
        exitItem.addActionListener(exitListener);
        menu.add(exitItem);
        return menu;
    }

    /**
     * Loading the image from a file
     */
    private static TrayIcon createTrayIconFromFile() {
        Image image =
          Toolkit.getDefaultToolkit().getImage("/temp/trayrealhowto.jpg");
        PopupMenu popup = createTrayMenu();
        TrayIcon ti = new TrayIcon(image, "Java System Tray Demo", popup);
        ti.setImageAutoSize(true);
        return ti;
    }

    /**
     * Loading the image from the classpath
     * if in a folder in a jar, remember to add the folder!
     * ex. /img/realhowto.jpg
     */
    private static TrayIcon createTrayIconFromResource()
      throws java.io.IOException {
      ClassLoader cldr = SysTrayDemo.class.getClassLoader();
      java.net.URL imageURL = cldr.getResource("trayrealhowto.jpg");
      Image image = Toolkit.getDefaultToolkit().getImage(imageURL);
      PopupMenu popup = createTrayMenu();
      TrayIcon ti = new TrayIcon(image, "Java System Tray Demo", popup);
      ti.setImageAutoSize(true);
      return ti;
    }

    /**
     * using a built-in icon
     * (may not work with some LAF or built-in icon, use with caution!)
     * we need to convert the icon to an Image
     */
    private static TrayIcon createTrayIconFromBuiltInIcon() {
        Icon icon = UIManager.getIcon("OptionPane.warningIcon");
        PopupMenu popup = createTrayMenu();
        Image image = iconToImage(icon);
        TrayIcon ti = new TrayIcon(image, "Java System Tray Demo", popup);
        ti.setImageAutoSize(true);
        return ti;
    }

    static Image iconToImage(Icon icon) {
          if (icon instanceof ImageIcon) {
              return ((ImageIcon)icon).getImage();
          } else {
              int w = icon.getIconWidth();
              int h = icon.getIconHeight();
              GraphicsEnvironment ge =
                GraphicsEnvironment.getLocalGraphicsEnvironment();
              GraphicsDevice gd = ge.getDefaultScreenDevice();
              GraphicsConfiguration gc = gd.getDefaultConfiguration();
              BufferedImage image = gc.createCompatibleImage(w, h);
              Graphics2D g = image.createGraphics();
              icon.paintIcon(null, g, 0, 0);
              g.dispose();
              return image;
          }
      }

    public static void main(String[] args) throws Exception {
        if (!SystemTray.isSupported()) {
            System.out.println
               ("System tray not supported on this platform");
            System.exit(1);
        }

        try {
            SystemTray sysTray = SystemTray.getSystemTray();
            trayIcon = createTrayIconFromFile();
            //trayIcon = createTrayIconFromResource();
            //trayIcon = createTrayIconFromBuiltInIcon();
            sysTray.add(trayIcon);
            trayIcon.displayMessage("Ready",
                "Tray icon started and tready", TrayIcon.MessageType.INFO);
        }
        catch (AWTException e) {
            System.out.println("Unable to add icon to the system tray");
            System.exit(1);
        }
    }
}
This code can load the icon from a file, a file in the classpath (resource) or use a built-in icon in the JDK.
blog comments powered by Disqus