Create Tray Icon : TrayIcon « Swing « Java Tutorial






import java.awt.AWTException;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Test {
  static Image image = Toolkit.getDefaultToolkit().getImage("images/tray.gif");

  static TrayIcon trayIcon = new TrayIcon(image, "Tester2");

  public static void main(String[] a) throws Exception {
    if (SystemTray.isSupported()) {
      SystemTray tray = SystemTray.getSystemTray();

      trayIcon.setImageAutoSize(true);
      trayIcon.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          System.out.println("In here");
          trayIcon.displayMessage("Tester!", "Some action performed", TrayIcon.MessageType.INFO);
        }
      });

      try {
        tray.add(trayIcon);
      } catch (AWTException e) {
        System.err.println("TrayIcon could not be added.");
      }
    }
  }
}








14.13.TrayIcon
14.13.1.Create Tray Icon
14.13.2.Add PropertyChangeListener to TrayIcon
14.13.3.Remove Icon from TrayIcon
14.13.4.Add mouse listener to TrayIcon
14.13.5.Add Mouse motion listener to TrayIcon
14.13.6.Using system tray
14.13.7.Demonstrating a System Tray That Responds to Selection