Search code examples
javaubuntugnome

Tray Icon not working on Ubuntu 16.04


I ran into an Issue with Java awt tray icons under Ubuntu Gnome 16.04:

The Icon is displayed in the top left corner of my screen and in the System Tray appears a black square. The MouseListener is also not working (neither on the icon nor on the black square).

Icon in the top left corner

System Tray

Here is my Code:

if (SystemTray.isSupported()) {
   Image image = ImageIO.read(EyeUNIFYlocal.class.getResource("/star.png"));
   TrayIcon trayIcon = new TrayIcon(image);
   trayIcon.setImageAutoSize(true);

   trayIcon.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
         System.out.println("Clicked");
      }
   });
   try {
      SystemTray.getSystemTray().add(trayIcon);
   } catch (AWTException ex) {
      System.err.println("Error while creating tray icon.");
   }
} else {
   System.err.println("Tray icons are not supported on this System.");
}

This code works fine on Windows 10.

Thank you in advance!


Solution

  • Java System Tray support doesn't exist for newer Linux distributions, mostly because of the changes from GtkStatusIcon to AppIndicator, and GTK2/3 changes as well (so, issues with JavaFX unless you install some additional libraries).

    Additionally, since you mentioned Gnome -- Gnome likes to "hide" the AppIndicator as "notifications", so there is an extension (top-icons) that lets you restore the indicators back to the top of the screen (instead of in a hidden drawer at the bottom left of the screen)

    If you want to display cross-platform system tray icons, I suggest the SystemTray project. There is an inbound 3.0 release soon (an API rewrite and better native support), but the older 2.x version should solve the problem you are having.