I'm trying to make a System Tray application on Ubuntu 18.04 using Java.
This is the code that I'm executing:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class App {
    static{
        System.setProperty("java.awt.headless", "false");
    }
    public static void main(String[] args) {
//       if(!SystemTray.isSupported()){
//           System.out.println("System Tray is not supported.");
//           return;
//       }
       final PopupMenu popup = new PopupMenu();
       Image img = Toolkit.getDefaultToolkit().createImage("/path/img.png");
       final TrayIcon trayIcon = new TrayIcon(img);
       final SystemTray systemTray = SystemTray.getSystemTray();
       //create components of system tray
        trayIcon.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                System.out.println("In here!");
                trayIcon.displayMessage("Test","Some action happened",TrayIcon.MessageType.INFO);
            }
        });
        try{
            systemTray.add(trayIcon);
        }catch(AWTException e){
            System.out.println("TrayIcon could not be added.");
        }
    }
}
I commented out the isSupported() method test snippet because I kept getting "System tray is not supported".
The Exception I'm getting is:
Exception in thread "main" java.lang.UnsupportedOperationException: The system tray is not supported on the current platform. at java.awt.SystemTray.getSystemTray(SystemTray.java:186) at App.main(App.java:16)
Any idea how I would make it supported? Also, if anyone has a MacOS device, can you try it, and let me know if it works? Thanks!
Gnome 3.28 (used in Ubuntu 18.04) removed the System tray. There is a software called TopIcon Plus Gnome Shell Exetension which returns the System tray. I tested the code, and the effects were as expected. An icon was placed in the Global bar.
https://extensions.gnome.org/extension/1031/topicons/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With