JAVA6 new features---a simple way to generate a pallet (tray) icon in a JAVA desktop application __java

Source: Internet
Author: User

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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.