Advertisment

Run Java Apps from SysTray 

author-image
PCQ Bureau
New Update

A Java programmer often misses the ease with which a VB or Visual C++ application can integrate with the Windows desktop. This is because Java, at its core, being platform independent tries to provide the most common UI components that are expected to be available across various OSs. Take for instance, a system tray-the bottom right corner of the Windows taskbar. Such a system tray is also available in rich desktop environments-GNOME and KDE-on Linux but is missing in FVWM, yet another graphical desktop for Linux. 

Advertisment

All this is fine, if you deploy a cross platform graphical application in Java. But what if you need to develop for a specific platform, say, for Windows? Windows users expect quite an intuitive behavior from the applications. Does that mean you can't leverage your expertise in Java and have to hop to Visual Basic or C++? The answer is No, for JDIC is here. JDIC is a Java desktop project at www.java.net that focuses on using native and platform-specific applications and facilities. According to the site, “JDIC provides Java applications with access to facilities provided by the native desktop such as the mailer, the browser and registered document viewing applications. Additionally it provides the mechanisms by which Java applications can integrate into the native desktop such as registering Java applications as document viewers on the desktop and creating installer packages.” Here we look at how to install a Java application on the Windows system tray. 

Steps to system tray integration

Direct Hit!
Applies to:

Java programmers
USP: Create a demo app that installs itself in the system tray
Links:

http://java.netwww.java.sun.com 
Advertisment

We assume that you are well versed with developing GUI applications with Java Swing. To install a Java application on the system tray, all you need to do is:

  • Construct a JPopupMenu (Java Swing)
  • Construct one or more JMenuItems and add them to the JPopupMenu (Java Swing)
  • Create an ImageIcon (Java Swing)
  • Construct a TrayIcon with the JPopupMenu as a pa rameter (JDIC specific)
  • Get a reference (a SystemTray object) to the Windows System Tray (JDIC specific)
  • Add the TrayIcon to the SystemTray (JDIC specific)

The code for the process is as follows.

Advertisment

// step 1



JPopupMenu menu = new JPopupMenu("Menu");

// step 2



JMenuItem menuItem1 = new JMenuItem("Menu 1");


menu.add(menuItem1);

// repeat step 2 to add more menu items to the JPopupMenu

Advertisment

//step 3



ImageIcon icon = new ImageIcon("icon.jpg");

// step 4



TrayIcon trayIcon = new TrayIcon(icon, "Hello System Tray", menu); 

// step 5



SystemTray tray = SystemTray.getDefaultSystemTray( );

Advertisment

// step 6



tray.addTrayIcon(trayIcon);

Note that the parameters to the TrayIcon( ) constructor are the ImageIcon object, tooltip (that will show when you move the mouse over the tray icon) and the

JPopupMenu respectively. 

Following is the code for a ready to compile and run Java program, which will install on the system tray. 

Advertisment

import javax.swing.*;



import org.jdesktop.jdic.tray.*;


import java.awt.event.*;

public class SystemTrayDemo extends JFrame{

public SystemTrayDemo(){

Advertisment

JPopupMenu menu = new JPopupMenu("Menu");



JMenuItem menuItem1 = new JMenuItem("Menu1");


menu.add(menuItem1);

JMenuItem menuItem2 = new JMenuItem("Menu2");



menu.add(menuItem2);

JMenuItem menuItem3 = new JMenuItem("Menu3");



menu.add(menuItem3);

JMenuItem menuItem4 = new JMenuItem("Exit");



menu.add(menuItem4);

menuItem4.addActionListener(new ActionListener() {



public void actionPerformed(ActionEvent evt) {


System.exit(0);


}});

ImageIcon icon = new ImageIcon("icon.jpg");



TrayIcon trayIcon = new TrayIcon(icon, "Hello System Tray", menu); 

SystemTray tray = SystemTray.getDefaultSystemTray();



tray.addTrayIcon(trayIcon);


}


public static void main(String<> args){


try {


javax.swing.UIManager.setLookAndFeel("com.sun.java.swing. plaf.windows.WindowsLookAndFeel");





catch(Exception e) {


System.out.println(e);


}





new SystemTrayDemo(); 


}}










Save the code as SystemTrayDemo.java in a directory, systray (say). Create/download any JPEG file in this directory (to display the tray icon) and call it

icon.jpg. 

Compile and run



Download the J2SE SDK 5.0 Update 1 for Windows from www.java.sun.com. Go to https://jdic.dev.java.net/servlets/ProjectDocumentList

and download the JDIC package. Click on jdic-0.8.8 and download the file named jdic-0.8.8-bin-windows.zip. Unzip the archive and copy files named jdic.dll, tray.dll and jdic.jar to the systray directory. Compile the code and execute it as:

javac -classpath jdic.jar;. SystemTrayDemo.java



java -cp jdic.jar;. SystemTrayDemo

You should be able to see a tray icon on the system tray. Right click on it to see the popup menu with menu items-Menu1, Menu2, Menu3 and Exit. 

To close or exit, click on Exit to call the System.exit( ) method-specified in the ActionListener for menuItem4 object. 

Shekhar Govindarajan



IT4Enterprise

Advertisment