View RSS Feed

Java AWT

How to create a system tray for your application

Rate this Entry
by , 12-09-2011 at 07:46 AM (4108 Views)
Since Java SE 6, programmers can add an icon for their applications in the system tray area of the operating system. A common usage scenario of the system tray is having a popup menu showing when users right click on the icon. The popup menu presents to users with common tasks that the application provides. When users double click on the icon, they will access the default action of the program. The screenshot below depicts a tray icon and popup menu of an application in the Taskbar notification area on Windows platform:

Name:  screenshot1.PNG
Views: 1160
Size:  6.4 KB
Figure: The popup menu displayed when clicking on an icon on the system tray

With the new API has been added since Java SE 6, adding a tray icon using Java is simple and easy, as you go further through this article. You will be going through some steps to access the system tray, add an icon, create a popup menu, and handle double click on the tray icon.

The API you need to know

There are two classes in the package java.awt you need to use when implementing a system tray:

  • The SystemTray class: represents the system tray of the operating system. You cannot create an instance of this class. There’s only a single instance of this class can exist and it can be obtained by calling the static SystemTray.getSystemTray()method. You should call the static method SystemTray.isSupported() to check that the operating system supports a system tray. The method returns false if the system tray is not supported. A tray icon can be added to the tray by using the add(java.awt.TrayIcon) method. The icon can be removed when it is no longer need by calling the remove(java.awt.TrayIcon) method.
  • The TrayIcon class: represents a tray icon that can be added to the system tray. You can specify a tooltip (text), an image, a popup menu for a TrayIcon. You can also register some listeners with the TrayIcon, such as mouse click event.


Access the system tray

Because some operating systems may not support the system tray feature, so it’s recommended to check whether the system tray is supported or not, before going further. The checking code is as follow:



Java Code: Check if the system is supported
        if (!SystemTray.isSupported()) {
            System.out.println("System tray is not supported");
            System.exit(0);
        }


If the method isSupported() returns false, we would exit the application. If it returns true, we can access the system tray, by invoking the following statement:


Java Code: Obtain the only one instance of the SystemTray object
        SystemTray tray = SystemTray.getSystemTray();


We have obtained an instance of the SystemTray, the tray object. Next, we can add interesting stuffs to it.

Creating a tray icon

It’s important to have a small and nice icon represents our application in the system tray area. To do so, we need to create an ImageIcon object from an image placed at the same folder as the source file (the image file name is icon.png):


Java Code: Creat a new image object
ImageIcon icon = new ImageIcon(SystemTrayApp.class.getResource("icon.png"));


Then we create a TrayIcon object by passing the ImageIcon object as its constructor argument:


Java Code: Create a new tray icon object
TrayIcon trayIcon = new TrayIcon(icon.getImage());


The image icon may not fit its own area on the system tray perfectly, so we should call the following method to let the system resizes the icon automatically if needed:


Java Code: Let the system resizes the image if needed
trayIcon.setImageAutoSize(true);


Set a tooltip text for the icon:


Java Code: Set tooltip text for the tray icon
trayIcon.setToolTip("System tray icon demo");


And add the icon to the tray:


Java Code: Add the tray icon to the system tray
       try {
           tray.add(trayIcon);
       } catch (AWTException e) {
           e.printStackTrace();
       }


Till now, if you run the application, an icon is placed on the system tray. It’s nice, but the icon does nothing if we click on it. So we will add a popup menu for right click and a message dialog for double click. The screenshot below shows the icon appears on the system tray:

Name:  screenshot2.PNG
Views: 1138
Size:  4.6 KB
Figure: The tooltip appears when the mouse cursor hovers over the tray icon

Adding popup menu

The following code will create a popup menu and two menu items, About and Exit, added to the popup:


Java Code: Create the popup menu
       PopupMenu popup = new PopupMenu();
       MenuItem menuAbout = new MenuItem("About");
       MenuItem menuExit = new MenuItem("Exit");
       popup.add(menuAbout);
       popup.add(menuExit);
       
       trayIcon.setPopupMenu(popup);


We add two event handlers for the two menu items About and Exit:


Java Code: Add event handlers for the popup menu's items
       menuAbout.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                JOptionPane.showMessageDialog(new JFrame(),
                        "Hello, I am a system tray icon.",
                        "System Tray App",
                        JOptionPane.INFORMATION_MESSAGE);               
            }
       });

       menuExit.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent arg0) {
               System.exit(0);
           }
       });


When users select About from the popup menu, a message dialog says “Hello, I am a system tray icon” appears. When users select Exit, the application terminates.

The following screen shot depicts the popup menu displayed when right clicking on the icon:

Name:  screenshot3.PNG
Views: 1091
Size:  2.4 KB
Figure: The popup menu displayed

And the message dialog appears:

Name:  screenshot4.PNG
Views: 1077
Size:  5.0 KB
Figure: The dialog appears when selecting About menu item

Click OK will close the dialog.

Handle double mouse click

We add a mouse listener to the tray icon in order to catch the event of mouse double clicking:


Java Code: Handle double mouse click event
       trayIcon.addMouseListener(new MouseAdapter() {
           public void mouseClicked(MouseEvent evt) {
               if (evt.getModifiers() == MouseEvent.BUTTON1_MASK) {
                   JOptionPane.showMessageDialog(new JFrame(),
                           "You have double clicked on me!",
                           "System Tray App",
                           JOptionPane.INFORMATION_MESSAGE);                   
               }
           }
       });


When users double click on the tray icon, a message dialog says “You have double clicked on me!” appears, as shown in the following screenshot:

Name:  screenshot5.PNG
Views: 1070
Size:  5.0 KB
Figure: The dialog appears when double-clicking on the tray icon

So far that’s all the steps to create the fundamental stuff for your application’s tray icon, adding the usability for your end users. Happy coding!

Submit "How to create a system tray for your application" to Facebook Submit "How to create a system tray for your application" to Digg Submit "How to create a system tray for your application" to del.icio.us Submit "How to create a system tray for your application" to StumbleUpon Submit "How to create a system tray for your application" to Google

Updated 12-09-2011 at 04:38 PM by Java AWT

Categories
Tray , Java AWT

Comments