Tuesday, 26 August 2008

Tray Icon in Java 6



I am much biased to web applications, the reason is that the new approaches that emrges every day facinated me a lot. But I am interested in destop based application (think client) also. When considering a Java application, taking the adavantage of OS System Tray/ Task Bar was not so easy until Java 6. But the latest release helped developers to a great extend. Here I have gicen a small Java program that can do the wonder of System Tray utilization, thanks Java 6.


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.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JOptionPane;

/**
* ApplicationExecuter
* @author Rajeev Nair
*/
public class ApplicationExecuter {

/**
* main method
* @param args - any command line arguments
*/
public static void main(String args[]){

if (!SystemTray.isSupported()){
System.out.println("System tray not supported on this platform");
System.exit(1);
} //end if

try {
doTrayStartUp();
} catch(Exception exp){
System.out.println("Exception occured while building the tray entry :(");
exp.printStackTrace();
} //end exception

} //end main

/**
* Creates a tray icon.
* @return TrayIcon
*/
private static TrayIcon createTrayIcon() {
// I have used an absolute path for image here .. :(
Image image = Toolkit.getDefaultToolkit().getImage("C:\\Apps\\Icons\\app.gif");
PopupMenu popup = createTrayMenu();
TrayIcon ti = new TrayIcon(image, "Example TrayIcon :)", popup);
ti.setImageAutoSize(true);
return ti;
} //end createTrayIcon

/**
* Creates a popup menu.
* @return PopupMenu
*/
private static PopupMenu createTrayMenu() {
// creating popup
PopupMenu menu = new PopupMenu();

// creating an entry element
MenuItem aboutItem = new MenuItem("About Example App!!!");
aboutItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Hello, :)",
"About Example App!!!", JOptionPane.INFORMATION_MESSAGE);
}
});
// addding an entry to popup menu
menu.add(aboutItem);

// adding a separator
menu.addSeparator();

// creating an entry element
MenuItem exitItem = new MenuItem("Exit");
exitItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// addding an entry to popup menu
menu.add(exitItem);
return menu;

} //end createTrayMenu


/**
* Building the tray entry
* @throws Exception
*/
private static void doTrayStartUp() throws Exception{

SystemTray sysTray = SystemTray.getSystemTray();
TrayIcon trayIcon = createTrayIcon();
sysTray.add(trayIcon);
trayIcon.displayMessage("Example App", "Example App started", TrayIcon.MessageType.INFO);

// add mouse listners
trayIcon.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {
if(e.getButton() == 1){
// if left button pressed
JOptionPane.showMessageDialog(null, "You have pressed the left button of the mouse.",
"Example App!!!", JOptionPane.INFORMATION_MESSAGE);
} //end if
}

public void mouseReleased(MouseEvent e) {}
});
} //end doTrayStartUp
}


Screen shots after running this program,

No comments: