Import javax.swing.*;
Import java.awt.*;
Import java.awt.event.*;
/**
* Example of making Java application generate pallet icons
* This code is tested on JDK1.6, Win2003 platform.
* @author: netjava.org
*/
public class Createtray
{
/** Create solid column * *
public static Createtray instance () {
if (NULL==CT) {
Ct=new Createtray ();
}
return CT;
}
Test
public static void Main (string[] args)
{
Createtray CT =instance ();
Tray icon file used in working directory
String iconfilename= "Netjava.gif";
PopupMenu Pop=ct.createpopup ();
if (!ct. Cretetrayicon (IconFileName, "My tray", pop)) {
SYSTEM.OUT.PRINTLN ("Cannot create pallet");
}
}
/**
* Create tray, such as successful return of true value
* @para trayimage: Create picture file name for tray icon
* @para Trayname:tray Display Name
* @para popup popupmenu on this tray
*/
public boolean Cretetrayicon (String trayimage,string trayname,popupmenu popup) {
Boolean iscreated=false;
Final TrayIcon TrayIcon;
if (systemtray.issupported ()) {
Systemtray tray = Systemtray.getsystemtray ();
Image image = Toolkit.getdefaulttoolkit (). GetImage (Trayimage);
TrayIcon = new TrayIcon (image, Trayname, popup);
Trayicon.setimageautosize (TRUE);
Create an action listener: left-click event
Final ActionListener al = new ActionListener () {
public void actionperformed (ActionEvent e) {
Trayicon.displaymessage ("Pallet event", "This double click event has been received", TrayIcon.MessageType.WARNING);
}
};
Trayicon.addactionlistener (AL);
try {
Tray.add (TrayIcon);
Iscreated=true;
catch (Awtexception e) {
System.err.println ("Unable to create pallet:" +e);
Iscreated=false;
}
}
return iscreated;
}
/**
* Create the right button pop-up menu on this tray
*/
Public PopupMenu createpopup () {
PopupMenu popup = new PopupMenu ();
MenuItem menuexit = new MenuItem ("Exit");
MenuItem Menuopen = new MenuItem ("open");
MenuItem menucancel = new MenuItem ("Cancel");
Create Exit Menu Listener
ActionListener Exitlistener = new ActionListener () {
public void actionperformed (ActionEvent e) {
System.exit (0);
}
};
Create Open Listener
ActionListener Openlistener = new ActionListener () {
public void actionperformed (ActionEvent e) {
Joptionpane.showinputdialog ("Please enter your password:");
}
};
Menuexit.addactionlistener (Exitlistener);
Menuopen.addactionlistener (Openlistener);
Popup.add (Menuopen);
Popup.add (Menucancel);
Popup.add (Menuexit);
return popup;
}
Private Createtray () {};
private static Createtray Ct=null;
}