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
- Make sure the platform supports SystemTray
- Load image for the trayicon
- 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.
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.
- Create PopumMenu
- Create one Menu and multiple MenuItems and organize them in a tree structure
- Attach Menu to PopumMenu
- 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.
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.
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
[…] Java TrayIcon Tutorial […]
… [Trackback]
[…] Find More on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]
… [Trackback]
[…] Information to that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]
… [Trackback]
[…] Find More here on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]
… [Trackback]
[…] Info on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]
… [Trackback]
[…] Information to that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]
… [Trackback]
[…] Find More Information here on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]
… [Trackback]
[…] Info to that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]
… [Trackback]
[…] Read More here to that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]
… [Trackback]
[…] Find More on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]
… [Trackback]
[…] Info on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]
… [Trackback]
[…] Read More Information here on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]
… [Trackback]
[…] Find More to that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]
… [Trackback]
[…] Read More on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]
… [Trackback]
[…] Read More Info here on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]
… [Trackback]
[…] Find More on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]
… [Trackback]
[…] Info on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]
… [Trackback]
[…] Find More Info here to that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]
… [Trackback]
[…] Read More Info here on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]
… [Trackback]
[…] Read More here on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]
… [Trackback]
[…] There you can find 16639 additional Info to that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]
… [Trackback]
[…] Read More Information here to that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]
… [Trackback]
[…] There you will find 62952 additional Info on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]
… [Trackback]
[…] Find More on that Topic: genuinecoder.com/java-trayicon-tutorial-with-popupmenu-and-images/ […]