Home > Java > Working with System Tray in Java

Working with System Tray in Java

Just thought that this is a useful handy piece of code, so am posting it. Its a demo of how to work with the System Tray in Java. The classes of interest are

java.awt.SystemTray

and

java.awt.TrayIcon

.

package com.hassan.javasystemtray;

import java.awt.AWTException;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class SystemTrayDemo {
	
   /**
    * 
    * @author Hassan Ali
    * https://jaspirations.wordpress.com
    * http://about.me/hassan.malik
    * hassan.ali.cs@gmail.com
    */
	
	private static PopupMenu getPopMenu() { 
		
		PopupMenu popUpMenu = new PopupMenu();
		MenuItem menuItem = new MenuItem("Hello User!");
		popUpMenu.add(menuItem);
		
		return popUpMenu;
	}
	
	public static void main(String[] args) { 
		
		SystemTray sysTray = SystemTray.getSystemTray();
		
		if(!SystemTray.isSupported())
			JOptionPane.showMessageDialog(null, "System Tray is not supported on this platform");
		else { 
			
			try {
			
				sysTray.add(new TrayIcon(new ImageIcon("C:\\faces_png.png").getImage(), 
						"Java System Tray Demo", SystemTrayDemo.getPopMenu()));
			}
			catch(AWTException awtException) { 
				
				System.out.println(awtException.toString());
			}
		}
		
	}

}
Advertisements
Categories: Java
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: