Have a systray icon (Windows) : TrayIcon « JDK 6 « Java






Have a systray icon (Windows)

   

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 javax.swing.JOptionPane;

public class Main {
  static TrayIcon trayIcon = null;

  public static void main(String[] args) throws Exception {

    SystemTray sysTray = SystemTray.getSystemTray();
    Image image = Toolkit.getDefaultToolkit().getImage("/a.jpg");

    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, "!", "User action",
            JOptionPane.INFORMATION_MESSAGE);
        trayIcon.displayMessage("Done", "!", 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);

    trayIcon = new TrayIcon(image, "Java System Tray Demo", menu);
    trayIcon.setImageAutoSize(true);
    sysTray.add(trayIcon);
    trayIcon.displayMessage("Ready", "started", TrayIcon.MessageType.INFO);

  }
}

   
    
    
  








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.Demonstrating a System Tray That Responds to Selection
7.Add PropertyChangeListener to TrayIcon
8.System tray icon
9.This program demonstrates the system tray API.