Demonstrating a System Tray That Responds to Selection : TrayIcon « JDK 6 « Java






Demonstrating a System Tray That Responds to Selection

    

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class ActiveTray {
  public static void main(String args[]) throws Exception{
    if (SystemTray.isSupported() == false) {
      System.err.println("No system tray available");
      return;
    }
    final SystemTray tray = SystemTray.getSystemTray();
    PropertyChangeListener propListener = new PropertyChangeListener() {
      public void propertyChange(PropertyChangeEvent evt) {
        TrayIcon oldTray[] = (TrayIcon[]) evt.getOldValue();
        TrayIcon newTray[] = (TrayIcon[]) evt.getNewValue();
        System.out.println(oldTray.length + " / " + newTray.length);
      }
    };
    tray.addPropertyChangeListener("trayIcons", propListener);
    Image image = Toolkit.getDefaultToolkit().getImage("jpgIcon.jpg");
    PopupMenu popup = new PopupMenu();
    MenuItem item = new MenuItem("Hello, World");
    final TrayIcon trayIcon = new TrayIcon(image, "Tip Text", popup);
    ActionListener menuActionListener = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        trayIcon.displayMessage("Good-bye", "Cruel World", TrayIcon.MessageType.WARNING);
      }
    };
    item.addActionListener(menuActionListener);
    popup.add(item);
    ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        tray.remove(trayIcon);
      }
    };
    trayIcon.addActionListener(actionListener);
    tray.add(trayIcon);
  }
}

          
    
    
  








Related examples in the same category

1.Create Tray Icon
2.Remove Icon from TrayIcon
3.Add mouse listener to TrayIcon
4.Add Mouse motion listener to TrayIcon
5.Using system tray
6.Add PropertyChangeListener to TrayIcon
7.Have a systray icon (Windows)
8.System tray icon
9.This program demonstrates the system tray API.