Add mouse listener to TrayIcon : TrayIcon « JDK 6 « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » JDK 6 » TrayIconScreenshots 
Add mouse listener to TrayIcon
    

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;

public class SystemTrayDemo2 {
  public static void main(String[] args) {
    if (!SystemTray.isSupported()) {
      return;
    }
    SystemTray tray = SystemTray.getSystemTray();

    Dimension size = tray.getTrayIconSize();
    BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();

    g.setColor(Color.blue);
    g.fillRect(00, size.width, size.height);
    g.setColor(Color.yellow);
    int ovalSize = (size.width < size.height? size.width : size.height;
    ovalSize /= 2;
    g.fillOval(size.width / 4, size.height / 4, ovalSize, ovalSize);

    try {
      PopupMenu popup = new PopupMenu();
      MenuItem miExit = new MenuItem("Exit");
      ActionListener al;
      al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          System.out.println("Goodbye");
          System.exit(0);
        }
      };
      miExit.addActionListener(al);
      popup.add(miExit);

      TrayIcon ti = new TrayIcon(bi, "System Tray Demo #2", popup);

      al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          System.out.println(e.getActionCommand());
        }
      };
      ti.setActionCommand("My Icon");
      ti.addActionListener(al);

      MouseListener ml;
      ml = new MouseListener() {
        public void mouseClicked(MouseEvent e) {
          System.out.println("Tray icon: Mouse clicked");
        }

        public void mouseEntered(MouseEvent e) {
          System.out.println("Tray icon: Mouse entered");
        }

        public void mouseExited(MouseEvent e) {
          System.out.println("Tray icon: Mouse exited");
        }

        public void mousePressed(MouseEvent e) {
          System.out.println("Tray icon: Mouse pressed");
        }

        public void mouseReleased(MouseEvent e) {
          System.out.println("Tray icon: Mouse released");
        }
      };
      ti.addMouseListener(ml);

      MouseMotionListener mml;
      mml = new MouseMotionListener() {
        public void mouseDragged(MouseEvent e) {
          System.out.println("Tray icon: Mouse dragged");
        }

        public void mouseMoved(MouseEvent e) {
          System.out.println("Tray icon: Mouse moved");
        }
      };
      ti.addMouseMotionListener(mml);

      tray.add(ti);
    catch (AWTException e) {
      System.out.println(e.getMessage());
      return;
    }
  }

}

          
    
    
  
Related examples in the same category
1.Create Tray Icon
2.Remove Icon from TrayIcon
3.Add Mouse motion listener to TrayIcon
4.Using system tray
5.Demonstrating a System Tray That Responds to Selection
6.Add PropertyChangeListener to TrayIcon
7.Have a systray icon (Windows)
8.System tray icon
9.This program demonstrates the system tray API.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.