Posted by ixmal
on November 17, 2006 at 12:01 AM PST
Another one improvement of java.awt.TrayIcon class: the ability to detect if the system tray is available at the given moment.
Description of the problem
java.awt.SystemTray class was first introduced in JDK 6.0, there was only one method to check if the system tray and tray icons can be used:
isSupported(). The value returned by this method is constant for the given desktop/environment, for example, on Windows platform it always return
Another thing that can help the developers with the system tray is
AWTException thrown by
SystemTray.add(TrayIcon icon) method. According to the JavaDoc for this method, this exception is thrown "if the desktop system tray is missing". One of the common cases when the system tray is missing is when the notification area applet is removed from the GNOME desktop manually by user.
Recently I have faced another problem, on Windows. There are several projects that make possible to install a Java application as a Windows NT/2K/XP service. Some of such Java applications may interact with the user using
TrayIcons. Here the problem lies: Windows services may start before any user is logged in, so no taskbar and system tray are present.
isSupported() method is not enough. Developers should be able to detect not only whether Java supports the system tray on the given platform, but also whether the system tray is currently present and tray icons can be added into it.
The most probable name for the new method will be
isAvailable. Additionally, it may be useful to have another method in the
SystemTray class that works the following way: if the system tray is available, adds the given tray icon into it, if not - waits until the tray becomes available and then adds the icon.
AWTException is not required for this case.
Introducing a new property
There is another problem to be resolved to make the using of tray icons easier. Consider an application like an instant messenger that is run as an icon in the system tray, without any other visible window. If the system tray is removed or made unavailable this application may lose some of its functionality.
Alternatively, using a property that changes its value when the system tray becomes available/unavailable, enables an application to listen for that property change and behave correspondingly. For example, an application can show some toplevel window.
There is already a property called 'trayIcons' in the
SystemTray class. The new property will be accessible as a parameter of the
java.beans.PropertyChangeListener interface instance by using the same methods: