System Tray Example

When I started Java, I always wondered how do I gonna create system tray for my program. It took some time for me to figure it out. In java, we can create system tray with the help of AWT package. Currently, swing package don’t support the system tray.

The main class named ‘MainFrame’ with main method that will create window & initialize the Utility(system tray) class.
Full source code below :

package tray.ui;

import javax.swing.JFrame;

/**
 *
 * @author PJ
 */
public class MainFrame {

    Utility utility;
    JFrame frame;

    public MainFrame() {
        initComponent();
    }

    public void initComponent() {
        frame = new JFrame("|| System Tray Example ||");
        frame.pack();
        utility = new Utility(this);
        frame.setSize(400, 300);
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        new MainFrame();
    }
}

Now, we have JFrame as window, it’s time to declare a class named ‘Utility’.

package tray.ui;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.Menu;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.ImageIcon;

/**
 *
 * @author PJ
 */
public class Utility {

    SystemTray systemTray;
    TrayIcon trayIcon;
    MainFrame mainFrame;

    public Utility(MainFrame mf) {
        mainFrame = mf;
        try {
            initSystemTray();
        } catch (AWTException awt) {
            System.out.println("Cannot initialize the system tray.");
            awt.printStackTrace();
        }
    }

    public void initSystemTray() throws AWTException {
        if (!SystemTray.isSupported()) {
            System.out.println("...system tray is not supported...");
            return;
        }

        String toolTip = "By : Prajin Maharjan";
        PopupMenu popup = new PopupMenu();
        trayIcon = new TrayIcon(createTrayIcon("/tray/ui/icon.png"));
        trayIcon.setImageAutoSize(true);
        trayIcon.setToolTip(toolTip);
        systemTray = SystemTray.getSystemTray();

        //--system tray items
        Menu table = new Menu(" Show Table ");
        MenuItem table1 = new MenuItem(" Table 1 ");
        MenuItem table2 = new MenuItem(" Table 2 ");
        MenuItem list1 = new MenuItem(" List 1");
        MenuItem list2 = new MenuItem(" List 2 ");
        MenuItem helpContentMenu = new MenuItem(" Help Content ");
        MenuItem exitMenu = new MenuItem(" Exit! ");
        
        table.add(table1);
        table.add(table2);

        popup.add(table);
        popup.addSeparator();
        popup.add(list1);
        popup.add(list2);
        popup.addSeparator();
        popup.add(helpContentMenu);
        popup.addSeparator();
        popup.add(exitMenu);

        trayIcon.setPopupMenu(popup);
        try {
            systemTray.add(trayIcon);
        } catch (AWTException awte) {
            System.out.println("System tray icon could not be added.");
            return;
        }

        table1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });

        table2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
    }

    public static Image createTrayIcon(String path) {
        URL imageURL = null;
        try {
            imageURL = Utility.class.getResource(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (imageURL == null) {
            System.err.println("Resource not found " + path);
            return null;
        } else {
            return (new ImageIcon(imageURL).getImage());
        }
    }

    public void isClose() {
        systemTray.remove(trayIcon);
    }
}

The output should look like :

System Tray Example Image
System Tray Example

Leave a comment

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.