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

Comments

50 responses to “Java TrayIcon Tutorial (With popupmenu and images)”

  1. … [Trackback]

    […] Find More on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  2. … [Trackback]

    […] Information to that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  3. … [Trackback]

    […] Find More here on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  4. … [Trackback]

    […] Info on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  5. … [Trackback]

    […] Information to that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  6. … [Trackback]

    […] Find More Information here on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  7. … [Trackback]

    […] Info to that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  8. … [Trackback]

    […] Read More here to that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  9. … [Trackback]

    […] Find More on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  10. … [Trackback]

    […] Info on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  11. … [Trackback]

    […] Read More Information here on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  12. … [Trackback]

    […] Find More to that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  13. … [Trackback]

    […] Read More on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  14. … [Trackback]

    […] Read More Info here on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  15. … [Trackback]

    […] Find More on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  16. … [Trackback]

    […] Info on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  17. … [Trackback]

    […] Find More Info here to that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  18. … [Trackback]

    […] Read More Info here on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  19. … [Trackback]

    […] Read More here on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  20. … [Trackback]

    […] There you can find 16639 additional Info to that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  21. … [Trackback]

    […] Read More Information here to that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  22. … [Trackback]

    […] There you will find 62952 additional Info on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  23. … [Trackback]

    […] Find More on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  24. … [Trackback]

    […] Here you will find 53662 additional Information to that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  25. … [Trackback]

    […] Find More Information here to that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  26. … [Trackback]

    […] Find More Info here on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  27. … [Trackback]

    […] Find More Information here to that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  28. … [Trackback]

    […] Read More here on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  29. … [Trackback]

    […] Find More on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  30. … [Trackback]

    […] Find More Information here to that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  31. … [Trackback]

    […] Information on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  32. … [Trackback]

    […] Information on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  33. … [Trackback]

    […] Here you can find 6382 more Information on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  34. … [Trackback]

    […] Find More Information here to that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  35. … [Trackback]

    […] Info to that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  36. … [Trackback]

    […] Read More on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]

  37. comment obtenir la prescription kamagra en ligne

    kamagra informations par courrier

  38. purchase enclomiphene purchase uk

    buying enclomiphene canada low cost

  39. order androxal generic lowest price

    cheap androxal cheap now

  40. discount flexeril cyclobenzaprine cheap melbourne

    cheap flexeril cyclobenzaprine buy online uk

  41. dutasteride uk do you need prescription

    foreign drug purchase without prescription dutasteride

  42. buy gabapentin canadian online pharmacy

    how to order gabapentin generic gabapentins

  43. order fildena for sale usa

    purchase fildena canada generic

  44. buy cheap itraconazole price australia

    buy cheap itraconazole canada suppliers

  45. buy staxyn usa mastercard

    discount staxyn cheap alternatives

  46. ordering avodart cheap next day delivery

    cheapest buy avodart no prescription needed

  47. buy rifaximin buy in london

    get rifaximin generic form

  48. online order xifaxan no prescription needed

    buy xifaxan generic mastercard

  49. kamagra a pナ册s noc

    kamagra bez rx

Leave a Reply