SystemTray class represents the system tray for a
desktop. On Microsoft Windows it is referred to as the "Taskbar
Status Area", on Gnome it is referred to as the "Notification
Area", on KDE it is referred to as the "System Tray". The system
tray is shared by all applications running on the desktop.
On some platforms the system tray may not be present or may not
be supported, in this case SystemTray#getSystemTray()
throws UnsupportedOperationException. To detect whether the
system tray is supported, use SystemTray#isSupported.
The SystemTray may contain one or more TrayIcons, which are added to the tray using the add method, and removed when no longer needed, using the
remove. TrayIcon consists of an
image, a popup menu and a set of associated listeners. Please see
the TrayIcon class for details.
Every Java application has a single SystemTray
instance that allows the app to interface with the system tray of
the desktop while the app is running. The SystemTray
instance can be obtained from the getSystemTray method.
An application may not create its own instance of
SystemTray.
The following code snippet demonstrates how to access and customize the system tray:
TrayIcon trayIcon = null;
if (SystemTray.isSupported()) {
// get the SystemTray instance
SystemTray tray = SystemTray.getSystemTray;
// load an image
java.awt.Image image = Toolkit.getDefaultToolkit().getImage (...);
// create a action listener to listen for default action executed on the tray icon
java.awt.event.ActionListener listener = new ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
// execute default action of the application
// ...
}
};
// create a popup menu
java.awt.PopupMenu popup = new PopupMenu();
// create menu item for the default action
MenuItem defaultItem = new MenuItem(...);
defaultItem.addActionListener(listener);
popup.add(defaultItem);
/// ... add other items
// construct a TrayIcon
trayIcon = new TrayIcon(image, "Tray Demo", popup);
// set the TrayIcon properties
trayIcon.addActionListener(listener);
// ...
// add the tray image
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println(e);
}
// ...
} else {
// disable tray option in your application or
// perform other actions
...
}
// ...
// some time later
// the application state has changed - update the image
if (trayIcon != null) {
trayIcon.setImage(updatedImage);
}
// ...
TrayIcon
| Modifier and Type | Field and Description |
|---|---|
| private PropertyChangeSupport | |
| private int | |
| private static final TrayIcon[] | |
| private Vector | |
| private transient SystemTrayPeer | |
| private static SystemTray |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| pack-priv synchronized void | |
| public synchronized void | addPropertyChangeListener(String
the specified property propertyName, PropertyChangeListener the property change listener to be added listener)Adds a |
| private void | firePropertyChange(String
the property whose value has changed propertyName, Object the property's previous value oldValue, Object the property's new value newValue)Support for reporting bound property changes for Object properties. |
| private synchronized PropertyChangeSupport | Returns: the current PropertyChangeSupport for thisSystemTrayReturns the current PropertyChangeSupport instance |
| public synchronized PropertyChangeListener[] | Returns: all of thePropertyChangeListeners associated with
the named property; if no such listeners have been added or
if propertyName is null or invalid, an empty
array is returnedthe specified property propertyName)Returns an array of all the listeners that have been associated with the named property. |
| public static SystemTray | Returns: theSystemTray instance that represents
the desktop's tray areaGets the |
| public TrayIcon[] | Returns: an array of all tray icons added to this tray, or an empty array if none has been addedReturns an array of all icons added to the tray by this application. |
| public Dimension | Returns: the default size of a tray icon, in pixelsReturns the size, in pixels, of the space that a tray icon will occupy in the system tray. |
| private static void | |
| public static boolean | Returns: false if no system tray access is supported; this
method returns true if the minimal system tray access is
supported but does not guarantee that all system tray
functionality is supported for the current platformReturns whether the system tray is supported on the current platform. |
| public void | |
| public synchronized void | removePropertyChangeListener(String
the specified property propertyName, PropertyChangeListener the PropertyChangeListener to be removed listener)Removes a |
| changeSupport | back to summary |
|---|---|
| private PropertyChangeSupport changeSupport | |
| currentIconID | back to summary |
|---|---|
| private int currentIconID | |
| EMPTY_TRAY_ARRAY | back to summary |
|---|---|
| private static final TrayIcon[] EMPTY_TRAY_ARRAY | |
| icons | back to summary |
|---|---|
| private Vector<TrayIcon> icons | |
| peer | back to summary |
|---|---|
| private transient SystemTrayPeer peer | |
| systemTray | back to summary |
|---|---|
| private static SystemTray systemTray | |
| SystemTray | back to summary |
|---|---|
| private SystemTray() Private | |
| add | back to summary |
|---|---|
| public void add(TrayIcon trayIcon) throws AWTException Adds a All icons added by the application are automatically
removed from the
| |
| addNotify | back to summary |
|---|---|
| pack-priv synchronized void addNotify() | |
| addPropertyChangeListener | back to summary | ||||||
|---|---|---|---|---|---|---|---|
| public synchronized void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) Adds a
If
| |||||||
| firePropertyChange | back to summary |
|---|---|
| private void firePropertyChange(String propertyName, Object oldValue, Object newValue) Support for reporting bound property changes for Object properties. This method can be called when a bound property has changed and it will send the appropriate PropertyChangeEvent to any registered PropertyChangeListeners. | |
| getCurrentChangeSupport | back to summary |
|---|---|
| private synchronized PropertyChangeSupport getCurrentChangeSupport() Returns the current PropertyChangeSupport instance
| |
| getPropertyChangeListeners | back to summary |
|---|---|
| public synchronized PropertyChangeListener[] getPropertyChangeListeners(String propertyName) Returns an array of all the listeners that have been associated with the named property.
| |
| getSystemTray | back to summary |
|---|---|
| public static SystemTray getSystemTray() Gets the
| |
| getTrayIcons | back to summary |
|---|---|
| public TrayIcon[] getTrayIcons() Returns an array of all icons added to the tray by this application. You can't access the icons added by another application. The returned array is a copy of the actual array and may be
modified in any way without affecting the system tray. To
remove a
| |
| getTrayIconSize | back to summary |
|---|---|
| public Dimension getTrayIconSize() Returns the size, in pixels, of the space that a tray icon will
occupy in the system tray. Developers may use this method to
acquire the preferred size for the tray icon before it is created.
For convenience, there is a similar method
| |
| initializeSystemTrayIfNeeded | back to summary |
|---|---|
| private static void initializeSystemTrayIfNeeded() | |
| isSupported | back to summary |
|---|---|
| public static boolean isSupported() Returns whether the system tray is supported on the current
platform. In addition to displaying the tray icon, minimal
system tray support includes either a popup menu (see Developers should not assume that all of the system tray
functionality is supported. To guarantee that the tray icon's
default action is always accessible, add the default action to
both the action listener and the popup menu. See the Note When implementing
| |
| remove | back to summary |
|---|---|
| public void remove(TrayIcon trayIcon) Removes the specified All icons added by the application are automatically
removed from the If
| |
| removePropertyChangeListener | back to summary |
|---|---|
| public synchronized void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) Removes a
If
| |