Java program to add tray icon and tray pop-up menu

Source: Internet
Author: User

After Java 1.6, you can easily add the tray icon and Tray Icon pop-up menu, the general code is as follows:

If (systemtray. issupported () {// determine whether the system tray URL is supported = This. getclass (). getresource ("/images/trayicon.gif"); // obtain the urlimageicon icon of the image = new imageicon (URL); // instantiate the image object image = icon. getimage (); // obtain the image object trayicon = new trayicon (image); // create a tray icon trayicon. addmouselistener (New mouseadapter () {// Add the mouse adapter public void mouseclicked (mouseevent E) for the tray {// mouse event if (E. getclickcount () = 2) {// determine whether the mouse has been double-clicked showframe (); // call the method display form}); trayicon. settooltip ("System Tray"); // Add the tooltip text popupmenu = new popupmenu (); // create the pop-up menu menuitem exit = new menuitem ("exit "); // create a menu item // response method exit. addactionlistener (New actionlistener () {public void actionreceivmed (actionevent e) {// todo auto-generated method stub}); popupmenu. add (exit); // Add a menu item trayicon for the pop-up menu. setpopupmenu (popupmenu); // Add systemtray = systemtray to the tray icon. getsystemtray (); // obtain the system tray object try {systemtray. add (trayicon); // Add a tray icon to the system tray} catch (exception e) {e. printstacktrace ();}}

The showframe () method is called to display the main window. Generally:

Public void showframe () {This. setvisible (true); // display the form this. setstate (frame. Normal );}

Notes;

1. images that do not support ICO are most suitable for JPG, GIF, PNG, and 16*16 sizes.

2. JDK must be later than 1.6.

3. This code is generally placed in the constructor. You can add the response method in the pop-up menu.


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.