Creating tray icons in Java

A tray icon is a long-lived application that sits on your computer desktop's system tray. Figure 1 shows some examples of tray icons in the authors system tray.
![]() Figure 1 Examples of tray icons in a system tray |
Typical examples of tray icons are e-mail watcher, buddy notifier in a chatroom, alarm, weather, sticky-notes, and RSS. A typical tray icon has the following properties and behaviors:
Figure 2 A tray icon's tooltip |
Again, the tip may change depending on the event of the tray icon. Returning to our Biff example, the tip may indicate the status of an Inbox by either show the number of new mail received or empty, if there are no new mail.
Until recently, it has been quite difficult to create Java based tray icons applications. JavaSE 6 (aka Mustang) now supports tray icon development with the TrayIcon API. Here are the steps to create your first tray icon application:
- You will first need to check if the system tray functionality is supported on your operating system. SystemTray is currently supported on Sun's JDS, KDE, Gnome and Windows. Once we have verified that the system tray is supported, we get an instance of SystemTray via a factory method. The following is a code snippet to show you how this is done:
//Check
if SystemTray is supported. If not supported we
exit |
- A TrayIcon is created with 3 pieces of information; you need to specify an image (Image) to represent it in the system tray. The image is mandatory. The second piece of information is the tooltip text, and the third piece of information is a popup menu (PopupMenu). The last two items are optional.
//Load an image for the tray icon. loadImage()
is a private method |
- Add an action listener to receive action event or left mouse click. The tray icon can receive mouse and mouse motion events as well. When you left click on your mouse on a tray icon, it will generate a ActionEvent. But if you are also planning to use the popup menu like we did above, then you need to name the generated ActionEvent with the setActionCommand() method.
//Give a name for the left click ActionEvent |
- The event handle method will look something like this:
//Give a name for the left click ActionEvent |
- Once we have completed the setup of our trayIcon instance, we can now install it on the system tray like so:
systemTray.add (trayIcon); |
- You can also display a popup message like informing the user that a high priority e-mail has arrived. This is different from tooltip in that for a tooltip to be displayed, you have to move your mouse over a tray icon. Popup messages are control programatically. Figure 3 shows how a system tray's message box looks like.
Figure 3 Displaying a tray icon message |
For example to display a "Updating feeds..." message as information, do the following:
displayMessage ("Update", "Updating feeds...", TrayIcon.INFO); |
The following is an example of an extremely simple RSS tray icon. The tray icon gets its feed from the planetnetbeans.org Web site. A left click will trigger the tray icon to retrieve the latest feed from the planetnetbeans.org Web site. A right click will list all the feeds. You can get the RSS tray icon source here. This is a NetBeans project directory (which is based on Ant). You can either use NetBeans--or any other editor or IDE--to open and build it .
Unzip the source. To run the the RSS tray icon, change directory into rssfeed/dist directory and type the following:
java -jar rssfeed.jar |
or the following if you are using Windows
javaw -jar rssfeed.jar |
Note:
rssfeed.jar is dependent on ";color:red;mso-ansi-language:en"="">rome-0.8.jar and ";color:red;mso-ansi-language:="" en"="">jdom.jar.
which can be found in ";color:red;mso-ansi-language:en"="">rssfeed/dist directory as well.
Lee Chuk-Munn has been programming in the Java language since 1996, when he first joined Sun Microsystems in Hong Kong. He currently works as a senior developer consultant and technology evangelist for Technology Outreach at Sun in Singapore. Chuk's focus is in Java APIs, Java EE, Java SE, and Java ME. Chuk graduated in 1987 from the Royal Melbourne Institute of Technology in Melbourne, Australia, where his favorite subject was compiler theory.