Java TrayIcon Tutorial (With popupmenu and images)

Java/JavaFX TrayIcon Tutorial

Trayicons are really useful widgets for interacting with the application when the main dialog is minimized/hidden or the application is running in the background. In this tutorial, let’s see how to add a Java Tray Icon with example code.

Note: JavaFX does not have a separate TrayIcon system. If your project is using JavaFX, then the tutorial is still fully applicable. Because the java.awt.SystemTray can be used for JavaFX as well!

Creating a simple TrayIcon

The first step on adding a TrayIcon is checking whether the platform supports it. As you know, java supports a wide variety of platforms and not all platforms will support SystemTray feature. So, the first step on configuring it is to check if it is really supported. Let’s see the basic algorithm here

  1. Make sure the platform supports SystemTray
  2. Load image for the trayicon
  3. Create trayicon object and add it to the tray

Let’s see how it is done in code.

public void createTrayIcon() {
  //Check for SystemTray support
  if (!SystemTray.isSupported()) {
    System.err.println("System tray feature is not supported");
    return;
  }

  //Get system tray object
  SystemTray tray = SystemTray.getSystemTray();

  //Create TrayIcon instance
  Image image = ImageIO.read(TrayIconService.class.getResourceAsStream("/path/to/your/icon.png"));
  TrayIcon trayIcon = new TrayIcon(image, "Genuine Coder", null);
  trayIcon.setImageAutoSize(true);

  //Attach TrayIcon to SystemTray
  tray.add(trayIcon);
}

and…That’s it. When you run the code, you will get a nice TrayIcon in your system’s taskbar. Below, you can find the output of running the code on my system.

Java TrayIcon on windows 10 taskbar

Creating TrayIcon with PopupMenu

Now, let’s see how to add a PopupMenu into the TrayIcon to add some functionality. Let’s iterate the steps first.

  1. Create PopumMenu
  2. Create one Menu and multiple MenuItems and organize them in a tree structure
  3. Attach Menu to PopumMenu
  4. Attach PopupMenu to TrayIcon

Basically, we need to prepare the MenuItems as we intend to and then attcah it to the PopupMenu. As an example, let’s create the following menu.

Java TrayIcon With PopupMenu

public void createTrayIcon() {
  //Check for SystemTray support
  if (!SystemTray.isSupported()) {
    System.err.println("System tray feature is not supported");
    return;
  }
  SystemTray tray = SystemTray.getSystemTray();
  Image image = ImageIO.read(TrayIconService.class.getResourceAsStream("/path/to/your/icon.png"));
  TrayIcon trayIcon = new TrayIcon(image, "Genuine Coder", null);
  trayIcon.setImageAutoSize(true);

  //Create root menu
  PopupMenu rootMenu = new PopupMenu();
  
  //Add about and restart items
  MenuItem about = new MenuItem("About");
  rootMenu.add(about);
  Menu restartMenu = new Menu("Restart");
  rootMenu.add(restartMenu);

  //Add sub-items to server
  MenuItem restartClient = new MenuItem("Client");
  MenuItem restartServer = new MenuItem("Server");
  restartMenu.add(restartClient);
  restartMenu.add(restartServer);

  //Attach to trayIcon
  trayIcon.setPopupMenu(rootMenu);

  tray.add(trayIcon);
}

Adding event handling to the TrayIcon

Now, let’s see how to attach event-handlers to the TrayIcon menu. As an example, let’s show an alert when the “about” MenuItem is clicked.

about.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(null, "Wow! Java TrayIcon is working!");
    }
});

With this event-handler, when you click on the “about” MenuItem in the TrayIcon menu, you will get the following alert.
TrayIcon action with event handler

Conclusion

In this tutorial, we have familiarized with Java TrayIcon and added PopupMenu and action listeners into it. TrayIcons can be customized even further by customizing the MenuItem. You can add special MenuItems like CheckBoxMenuItem to add switch behaviour into your SystemTray. You may also want to checkout the following articles

  1. How to send email using Java
  2. Convert JAR to EXE
Muhammed Afsal Villan
Muhammed Afsal Villan is an experienced full-stack developer, specialized in desktop and mobile application development. He also regularly publishes quality tutorials on his YouTube channel named 'Genuine Coder'. He likes to contribute to open-source projects and is always enthusiastic about new technologies.

24 COMMENTS