Oct 3, 2012

How to Create System Tray Icon in Java

System Tray Icon
In this tutorial, I will show you how to create a System Tray Icon in Java. Operating Systems normally provides a  place(program) for icons display, either for the programs that runs in background, such as services, or programs that continue to run while the window is closed. For example, if your computer supports advanced power management (APM), a Battery Meter icon may be displayed on the taskbar. In Windows its called a Status Area, others called it System Tray. 
package codecypherprojects;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.net.URL;
import javax.swing.*;

/**
 *
 * @author Khamis
 * 
 */

public class SystemTray {
    
    
    public SystemTray(){
        createSystemTray();
    
    }
    
        public static void main(String[] args) {
            new SystemTray();
    }


    protected  Image createImage(String path) {
       Image icon = new ImageIcon(getClass().getResource(path)).getImage();
       return icon;
    }

    private void createSystemTray() {
        
        // Make sure you check for tray icon support first
        if (!java.awt.SystemTray.isSupported()) {
            System.out.println("SystemTray is not supported");
            return;
        }
        
        // Create popup menu for tray icon info
        final PopupMenu popup = new PopupMenu();
        
        // Fetch the tray image icon
        Image img = createImage("/resource/icon.gif");
        ImageIcon icon = new ImageIcon(img);
        
        // Create the tray icon object
        final TrayIcon trayIcon = new TrayIcon(icon.getImage());
        
        // Create the system tray for tray the icon
        final java.awt.SystemTray tray = java.awt.SystemTray.getSystemTray();
 
        MenuItem aboutItem = new MenuItem("Tray Info");
        popup.add(aboutItem);
        MenuItem exitItem = new MenuItem("Exit");
        popup.add(exitItem);
         
        // Set the popup menu to the tray icon
        trayIcon.setPopupMenu(popup);
        
        // Catch possible exceptions when adding icon to the tray
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            System.out.println("Cannot add the TrayIcon!");
            
        }
          
            trayIcon.addActionListener(new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                      JOptionPane.showMessageDialog(null,
                        "This is the tray icon's info");
            }
        });
            
               aboutItem.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      JOptionPane.showMessageDialog(null,
                       "This dialog is all about system tray program");
            }
        });
               
               exitItem.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      tray.remove(trayIcon);
                       System.exit(0);
            }
        });
               
    }
}

No comments:

Post a Comment