Cross-platform SystemTray support for Swing/AWT, GtkStatusIcon, and AppIndicator on Java 8+
 
 
 
 
 
Go to file
Robinson 5e7928c6a6
added comments for SWT example
2024-02-14 23:58:06 +01:00
gradle/wrapper updated gradle 2024-02-14 23:56:39 +01:00
misc Added windows NATIVE L&F. 2023-12-18 16:36:09 +01:00
src/dorkbox/systemTray Runtime injection of macos screenDPI to fix access to sun.awt 2023-12-29 10:28:46 +01:00
src9 Fixed imports, updated license. 2023-01-15 00:49:31 +01:00
test-javaFx/dorkbox updated deps 2023-11-20 22:18:32 +01:00
test-normal updated resources, so that intellij doesn't complain when compiling test examples 2024-02-14 23:57:53 +01:00
test-resources/dorkbox updated resources, so that intellij doesn't complain when compiling test examples 2024-02-14 23:57:53 +01:00
test-swt/dorkbox added comments for SWT example 2024-02-14 23:58:06 +01:00
.gitignore Updated to use Gradle Utils for gradle/project update management 2019-05-13 15:18:32 +02:00
LICENSE updated license 2024-02-14 23:56:31 +01:00
LICENSE.Apachev2 Added lantern license 2014-12-30 00:22:52 +01:00
LICENSE.BSD3 Added other license files 2020-08-19 13:11:20 +02:00
LICENSE.CC0 Added other license files 2020-08-19 13:11:20 +02:00
LICENSE.MIT Changed license filenames to be lexicographical ordered 2014-11-16 15:24:50 +01:00
README.md Added windows NATIVE L&F. 2023-12-18 16:36:09 +01:00
build.gradle.kts Fixed issues with gradle build and test examples. 2024-02-14 23:56:22 +01:00
gradle.properties Added more gradle build properties 2021-12-13 09:27:51 -07:00
gradlew updated gradle 2024-02-14 23:56:39 +01:00
gradlew.bat updated gradle 2023-08-20 13:41:57 +02:00
settings.gradle.kts Hardcoded project name 2023-08-07 17:24:51 -06:00

README.md

SystemTray

Git Github Gitlab

Professional, cross-platform SystemTray support for Swing/AWT, GtkStatusIcon, and AppIndicator on Java 8+.

This library provides OS Native menus and Swing/AWT menus, depending on the OS and Desktop Environment and if AutoDetect (the default) is enabled.

  • Linux/Unix will automatically choose Native (GtkStatusIcon or AppIndicator) menus, Windows will choose Native (WindowsNotifyIcon with Swing menus), and MacOS will choose AWT.

  • Please note that the Native and AWT menus follow the specified look and feel of that OS and are limited by what is supported on the OS. Consequently they are not consistent across all platforms and environments.

  • In most cases on Linux/Unix, Native menus are used. In cases where libraries are missing or there are un-resolvable GTK version conflicts, we try to fallback to using Swing.

 

The following unique problems are also solved by this library:

  1. Sun/Oracle system-tray icons on Linux/Unix do not support images with transparent backgrounds
  2. Sun/Oracle system-tray and SWT system-tray implementations do not support app-indicators, which are necessary on different distributions of Linux/Unix
  3. GNOME3 desktop environments hide or remove entirely system tray icons (hidden from 3.16-3.25, removed from 3.26+)
  4. Sun/Oracle system-tray menus on Windows look absolutely horrid
  5. Sun/Oracle system-tray icons on Windows are hard-coded to a max size of 24x24 (it was last updated in 2006)
  6. Sun/Oracle AWT system-tray menus on MacOS do not respond to both mouse buttons, where native menus do
  7. Sun/Oracle AWT system-tray menus on MacOS do not support images, where native menus do
  8. Windows native menus do not support images attached to menu entries
  9. Windows menus do not support a different L&F from the running application
  10. Windows L&F is, by default, not the native L&F.
  11. java.awt.Desktop.getDesktop() is broken when using GTK3 or on MacOS.
  12. Windows, Linux, and MacOSX menus (native or otherwise) do not support HiDPI configurations
    1. Java8 is not DPI aware (so the icons + fonts must be scaled appropriately)
    2. Java 11+ is DPI aware, but must be explicitly enabled via native API calls
  13. Gnome/KDE environments no longer show system-tray icons without special shell extensions installed (this library will install and update them automatically)

This is for cross-platform use, specifically - linux 32/64/arm, mac 32/64/arm, and windows 32/64/arm. Java 8+

 
 

Linux

MacOS

Windows

   

Problems and Restrictions

  • JavaFX uses GTK2 for Java <8, and GTK2 or GTK3 for Java 9, and GTK3 for Java 10+. We try to autodetect this, and are mostly successful. In some situations where it doesn't work. Please set SystemTray.FORCE_GTK2=true;, or to change JavaFX (9+), use -Djdk.gtk.version=3 to solve this.

  • SWT can use GTK2, GTK3, or GTK4. We do not support GTK4, and recommend GTK3 (now the default for SWT) If you want to use something else, you must configure both SWT and the SystemTray to same, before SWT is initialized and only if there are problems with the autodetection, via SystemTray.FORCE_GTK2=true;.

  • AppIndicators under Ubuntu 16.04 (and possibly other distro's) will not work as a different user without extra work (ie: as a sudo'd user to root), since AppIndicators require a dbus connection to the current user's window manager. (see the Notes below for the details)

  • MacOSX is a special snowflake in how it handles GUI events, and so there are some bizzaro combinations of SWT, JavaFX, and Swing that do not work together. (see the Notes below for the details)

  • MacOSX original menus cannot display images attached to menu entries. The native implementation supports images. If forced to Swing instead, the tray-menu will no longer support the OS theme and transparancy.

  • Gnome 3: 3.16 - 3.25 (Fedora, Manjaro, Arch, etc) environments by default do not allow the SystemTray icon to be shown. This has been worked around and the tray icon will be placed next to the clock. A different workaround is to install the [Top Icons] (https://extensions.gnome.org/extension/1031/topicons/) plugin which moves icons from the notification drawer sometimes at the bottom left (collapsed) corner of the screen to the menu panel next to the clock.

  • Gnome 3: 3.26+ (Fedora, Manjaro, Arch, etc) environments by default do not allow the SystemTray icon to be shown. This has been worked around and the tray icon will be placed next to the clock. A different workaround is to install the Appindicator Support plugin which allows the addition of app-indicator icons where one would expect. Additionally, you will need to install libappindicator-gtk3.

  • ToolTips The maximum length is 64 characters long, and it is not supported on all Operating Systems and Desktop Environments. Specifically, Swing and GtkStatusIcon types support tray tooltips and menu tooltips. AppIndicator types do not support tooltips of any kind. Please note that Ubuntu uses AppIndicators!

  • Linux/Unix Menus Some Linux environments only support right-click to display the menu, and it is not possible to change the behavior.

  • Linux/Unix and java.awt.Desktop.getDesktop() Please use the dorkbox.util.Desktop class as a replacement, which will intelligently call the correct OS API to open a folder/directory, email, or browser. (Many thanks to QZ Tray for this).

  • WSL Windows Subsystem for Linux requires some extra work to get a tray icon showing correctly, either by starting java under windows (instead of WSL), or by adding an X-Server.

AutoDetect Compatibility List

OS Supported
Arch Linux + Gnome3
Manjaro Linux + KDE
ChromeOS x
Debian 8.5 + Gnome3
Debian 8.6 + Gnome3
Debian 9.5 + Gnome3
Debian 9.5 + KDE
Debian 9.5 + Cinnamon
Debian 9.5 + MATE
Debian 9.5 + LXDE
Debian 9.5 + XFCE
Elementary OS 0.3.2
Elementary OS 0.4
Elementary OS 5.0 *
Fedora 23
Fedora 24
Fedora 25
Fedora 25 KDE
Fedora 26
Fedora 27
Fedora 28
Fedora 29
Fedora 39
FreeBSD 11 + Gnome3
IGEL OS 9.5
Kali 2016
Kali 2017
LinuxMint 18
LinuxMint 19
LinuxMint 20
Ubuntu 12.04
Ubuntu 14.04
Ubuntu 16.04
Ubuntu 17.04
Ubuntu 17.10
Ubuntu 18.04
Ubuntu 20.04
Ubuntu 22.04
UbuntuGnome 16.04
UbuntuGnome 17.04
Kubuntu 23.10
XUbuntu 16.04
MacOSx 10.x
MacOSx 11.x
MacOSx 12.x
MacOSx 13.x
MacOSx 14.x
Windows XP
Windows 7
Windows 8.1
Windows 10
Windows 11
WSL 1 x
WSL 2 x
  • If it is working on an unlisted OS/DE, please let us know!!

Notes:

  • The compatibility list only applies while the SystemTray is in AutoDetect mode. Not all OSes support forcing a custom tray type.

  • Some Linux operating systems with GNOME 3 might require the installation of the app-indicator library as well. We usually provide feedback when this is necessary. (Arch, Fedora, etc)

  • The menu item callbacks occur on their own dispatch thread (instead of being on whatever OS's event dispatch thread), in order to provide consistent actions across all platforms. It is critical to make sure that access to Swing/etc that depend on running events inside their own EDT, are properly called. IE: SwingUtilities.invokeLater(). Do not use invokeAndWait() as weird GUI anomalies can happen.

  • Ubuntu 16.04+ with JavaFX require libappindicator1 because of JavaFX GTK and indicator panel incompatibilities. See more details. We attempt to fallback to using Swing in this situation.

  • Ubuntu 17.04, Java only supports the X11 backend. MIR is not supported.

  • Debian + GNOME 3, SystemTray works, but will only show in a tray via pressing SUPER+M.

  • MacOSX JavaFX (Java7) is incompatible with the SystemTray by default. See issue details.

    • To fix this do one of the following
      • Upgrade to Java 8
      • Add : -Djavafx.macosx.embedded=true as a JVM parameter
      • Set the system property via System.setProperty("javafx.macosx.embedded", "true"); before JavaFX is initialized, used, or accessed. NOTE: You may need to change the class (that your main method is in) so it does NOT extend the JavaFX Application class.
  • SWT builds for FreeBSD do not exist.

  • ElementaryOS 5.0+ removed support for appindicators by just not including a library. You can add it back with here.

  • Linux/Unix: If you want to run this library as a different user, you will need to launch your application via sudo su username /bin/sh -c "DBUS_SESSION_BUS_ADDRESS='unix:abstract=/tmp/dbus-cLtEoBPmgC' XDG_CURRENT_DESKTOP=$XDG_CURRENT_DESKTOP program-name", where unix:abstract=/tmp/dbus-cLtEoBPmgC from /run/user/{uid}/dbus-session. You will also want to disable the root check + warnings via SystemTray.ENABLE_ROOT_CHECK=false; See issue for more details.

  • Linux/Unix: If you want to create a custom menu for the LAUNCHER icon, you must create a custom .desktop shortcut in order to create Actions. Credit to @tresf for figuring this out.

    For example:

    steam launcher screen shot

[Desktop Entry]
Name=Steam
Comment=Application for managing and playing games on Steam
Exec=/usr/bin/steam %U
Icon=steam
Terminal=false
Type=Application
Categories=Network;FileTransfer;Game;
MimeType=x-scheme-handler/steam;
Actions=Store;Community;Library;Servers;Screenshots;News;Settings;BigPicture;Friends;

And then the executable path and language translations are as follows:

[Desktop Action Store]
Name=Store
Name[de]=Shop
Name[es]=Tienda
Name[fr]=Magasin
Name[it]=Negozio
Name[pt]=Loja
Name[ru]=Магазин
Name[zh_CN]=商店
Name[zh_TW]=商店
Exec=steam steam://store

 
 

Customization parameters:

SystemTray.AUTO_SIZE   (type boolean, default value 'true')
 - Enables auto-detection for the system tray. This should be mostly successful.
 
 
SystemTray.APP_NAME   (type String, default value 'SystemTray')
 - Default name of the application, sometimes shows on tray-icon mouse over. Not used for all OSes, but mostly for Linux
   
 
SystemTray.FORCE_GTK2    (type boolean, default value 'false')
 - Forces the system tray to always choose GTK2 (even when GTK3 might be available).
 
 
SystemTray.PREFER_GTK3    (type boolean, default value 'true') 
 - Prefer to load GTK3 before trying to load GTK2.
    

SystemTray.FORCE_TRAY_TYPE   (type SystemTray.TrayType, default value 'AutoDetect')
 - Forces the system tray detection to be AutoDetect, GtkStatusIcon, AppIndicator, WindowsNotifyIcon, Swing, or AWT.
   This is an advanced feature, and it is recommended to leave it at AutoDetect.

 
SystemTray.AUTO_FIX_INCONSISTENCIES    (type boolean, default value 'true')
 -  Allows the SystemTray logic to resolve various OS inconsistencies for the SystemTray in different combinations

 
SystemTray.ENABLE_ROOT_CHECK    (type boolean, default value 'true')
 -  Allows the SystemTray logic to ignore if root is detected. Usually when running as root it will not work (because of 
    how DBUS operates), but in rare situations, it might work.
    This is an advanced feature, and it is recommended to leave as true 
 
 
SystemTray.SWING_UI    (type SwingUIFactory, default value 'null')
 - Allows the developer to provide a custom look and feel for the Swing UI, if defined. See the test example for specific use.
      
 
SystemTray.DEBUG    (type boolean, default value 'false')
 -  This property is provided for debugging any errors in the logic used to determine the system-tray type and initialization feedback.


SizeAndScalingLinux.OVERRIDE_MENU_SIZE    (type int, default value '0')
 - Allows overriding of the LINUX system tray MENU size (this is what shows in the system tray).


SizeAndScalingLinux.OVERRIDE_TRAY_SIZE    (type int, default value '0')
 - Allows overriding of the LINX system tray ICON size (this is what shows in the system tray).


SizeAndScalingMacOS.OVERRIDE_MENU_SIZE    (type int, default value '0')
 - Allows overriding of the MACOS system tray MENU size (this is what shows in the system tray).


SizeAndScalingMacOS.OVERRIDE_TRAY_SIZE    (type int, default value '0')
 - Allows overriding of the MACOS system tray ICON size (this is what shows in the system tray).


SizeAndScalingWindows.OVERRIDE_MENU_SIZE    (type int, default value '0')
 - Allows overriding of the WINDOWS system tray MENU size (this is what shows in the system tray).
 
 
SizeAndScalingWindows.OVERRIDE_TRAY_SIZE    (type int, default value '0')
 - Allows overriding of the WINDWOS system tray ICON size (this is what shows in the system tray).


GtkTheme.TRAY_IMAGE_SIZE_FALLBACK    (type int, default value '24')
 - Fallback for an unknown tray image size for GTK 


GtkTheme.TRAY_MENU_IMAGE_SIZE_FALLBACK    (type int, default value '16')
 - Fallback for an unknown tray menu image size for GTK

The test application is on Git, and a simple example is as follows:

    SystemTray.SWING_UI = new CustomSwingUI();

    SystemTray systemTray = SystemTray.get();
    if (systemTray == null) {
        throw new RuntimeException("Unable to load SystemTray!");
    }
 

    systemTray.setImage("grey_icon.png");
    systemTray.setStatus("Not Running");
    
    systemTray.getMenu().add(new MenuItem("Quit", new ActionListener() {
        @Override
        public
        void actionPerformed(final ActionEvent e) {
            systemTray.shutdown();
            //System.exit(0);  not necessary if all non-daemon threads have stopped.
        }
    })).setShortcut('q'); // case does not matter

 
 

Note: This project was heavily influenced by the excellent Lantern project (when it was Java based),
      *Many* thanks to them for figuring out AppIndicators via JNA.
      https://github.com/getlantern/lantern
Note: Gnome-shell users can install an extension to support placing the tray icon next to all 
      of other OS tray icons. By default, all tray icons go to a "Notification drawer" which 
      is initially hidden. 
Note: We have fixed the Swing notification tray on Linux (it no longer has a greyish background), however
      to facilitate this, a screen-shot is grabbed where the icon is. Because this must happen after the
      icon is placed, *sometimes* you can see this happen. Unfortunately this is the only way to fix
      this problem, and there are no other known workarounds outside of writing an X11 wrapper from scratch.

 
 

ISSUES:
      'Trying to remove a child that doesn't believe we're it's parent.'
      
      This is a known appindicator bug, and is rather old. Some distributions use 
      an OLD version of libappindicator, and will see this error. 
         See: https://github.com/ValveSoftware/steam-for-linux/issues/1077
      -----------------------   
         
         
      'gsignal.c: signal 'child-added' is invalid for instance 'xyz' of type 'GtkMenu''
      
      This is a known appindicator bug, and is rather old. Some distributions use an 
      OLD version of libappindicator, and will see this error. 
      
      The fallout from this issue (ie: menu entries not displaying) has been 
      *worked around*, so the menus should still show correctly.
         See: https://askubuntu.com/questions/364594/has-the-appindicator-or-gtkmenu-api-changed-in-saucy
      -----------------------   
         
         
      Gdk-WARNING XSetErrorHandler() called with a GDK error trap pushed. Don't do that.
      
      This is a bug specifically with JavaFX +GTK3. From what I can tell, this not something that can be fixed.
        (bugs)
        https://stackoverflow.com/questions/55446534/how-to-fix-java22494-gdk-warning
        https://bugs.openjdk.java.net/browse/JDK-8156779
        https://bugs.openjdk.java.net/browse/JDK-8211305

        (Source files)
        https://github.com/openjdk/jfx/blob/master/modules/javafx.graphics/src/main/java/com/sun/glass/ui/gtk/GtkApplication.java
        https://github.com/openjdk/jfx/blob/master/modules/javafx.graphics/src/main/native-glass/gtk/GlassApplication.cpp

        (Where the error occurs)
        https://gitlab.gnome.org/GNOME/gtk/-/blob/master/gdk/x11/gdkmain-x11.c
  

   

Maven Info

<dependencies>
    ...
    <dependency>
      <groupId>com.dorkbox</groupId>
      <artifactId>SystemTray</artifactId>
      <version>4.5</version>
    </dependency>
</dependencies>

Gradle Info

dependencies {
    ...
    implementation("com.dorkbox:SystemTray:4.5")
}

License

This project is © 2023 dorkbox llc, and is distributed under the terms of the Apache v2.0 License. See file "LICENSE" for further references.