This program demonstrates the system tray API. : 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 
This program demonstrates the system tray API.
 

/*
 This program is a part of the companion code for Core Java 8th ed.
 (http://horstmann.com/corejava)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import javax.swing.Timer;

/**
 * This program demonstrates the system tray API.
 
 @version 1.00 2007-09-22
 @author Cay Horstmann
 */
public class SystemTrayTest {
  public static void main(String[] args) {
    final TrayIcon trayIcon;

    if (!SystemTray.isSupported()) {
      System.err.println("System tray is not supported.");
      return;
    }

    SystemTray tray = SystemTray.getSystemTray();
    Image image = Toolkit.getDefaultToolkit().getImage("cookie.png");

    PopupMenu popup = new PopupMenu();
    MenuItem exitItem = new MenuItem("Exit");
    exitItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.exit(0);
      }
    });
    popup.add(exitItem);

    trayIcon = new TrayIcon(image, "Your Fortune", popup);

    trayIcon.setImageAutoSize(true);
    trayIcon.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        trayIcon.displayMessage("How do I turn this off?",
            "Right-click on the fortune cookie and select Exit.", TrayIcon.MessageType.INFO);
      }
    });

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

    final List<String> fortunes = readFortunes();
    Timer timer = new Timer(10000new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        int index = (int) (fortunes.size() * Math.random());
        trayIcon.displayMessage("Your Fortune", fortunes.get(index), TrayIcon.MessageType.INFO);
      }
    });
    timer.start();
  }

  private static List<String> readFortunes() {
    List<String> fortunes = new ArrayList<String>();
    try {
      Scanner in = new Scanner(new File("fortunes"));
      StringBuilder fortune = new StringBuilder();
      while (in.hasNextLine()) {
        String line = in.nextLine();
        if (line.equals("%")) {
          fortunes.add(fortune.toString());
          fortune = new StringBuilder();
        else {
          fortune.append(line);
          fortune.append(' ');
        }
      }
    catch (IOException ex) {
      ex.printStackTrace();
    }
    return fortunes;
  }

}

   
  
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.Have a systray icon (Windows)
9.System tray icon
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.