Class SystemTray
- java.lang.Object
-
- java.awt.SystemTray
-
public class SystemTray extends java.lang.ObjectThe
SystemTrayclass 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
SystemTraymay 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).TrayIconconsists of an image, a popup menu and a set of associated listeners. Please see theTrayIconclass for details.Every Java application has a single
SystemTrayinstance that allows the app to interface with the system tray of the desktop while the app is running. TheSystemTrayinstance 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:
TrayIcontrayIcon = null; if (SystemTray.isSupported()) { // get the SystemTray instance SystemTray tray = SystemTray.getSystemTray(); // load an imageImageimage =Toolkit.getDefaultToolkit().getImage(...); // create a action listener to listen for default action executed on the tray iconActionListenerlistener = newActionListener() { public voidactionPerformed(ActionEvente) { // execute default action of the application // ... } }; // create a popup menuPopupMenupopup = 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
