Class SystemTray
- java.lang.Object
-
- java.awt.SystemTray
-
public class SystemTray extends java.lang.ObjectThe
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
getSystemTray()
throwsUnsupportedOperationException
. To detect whether the system tray is supported, useisSupported()
.The
SystemTray
may contain one or moreTrayIcons
, which are added to the tray using theadd(java.awt.TrayIcon)
method, and removed when no longer needed, using theremove(java.awt.TrayIcon)
.TrayIcon
consists of an image, a popup menu and a set of associated listeners. Please see theTrayIcon
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. TheSystemTray
instance can be obtained from thegetSystemTray()
method. An application may not create its own instance ofSystemTray
.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 imageImage
image =Toolkit.getDefaultToolkit().getImage
(...); // create a action listener to listen for default action executed on the tray iconActionListener
listener = newActionListener
() { public voidactionPerformed
(ActionEvent
e) { // execute default action of the application // ... } }; // create a popup menuPopupMenu
popup = newPopupMenu
(); // create menu item for the default action MenuItem defaultItem = new MenuItem(...); defaultItem.addActionListener(listener); popup.add(defaultItem); /// ... add other items // construct a TrayIcon trayIcon = newTrayIcon
(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); } // ...- Since:
- 1.6
- See Also:
TrayIcon