46 * be supported, in this case {@link SystemTray#getSystemTray()}
47 * throws {@link UnsupportedOperationException}. To detect whether the
48 * system tray is supported, use {@link SystemTray#isSupported}.
49 *
50 * <p>The <code>SystemTray</code> may contain one or more {@link
51 * TrayIcon TrayIcons}, which are added to the tray using the {@link
52 * #add} method, and removed when no longer needed, using the
53 * {@link #remove}. <code>TrayIcon</code> consists of an
54 * image, a popup menu and a set of associated listeners. Please see
55 * the {@link TrayIcon} class for details.
56 *
57 * <p>Every Java application has a single <code>SystemTray</code>
58 * instance that allows the app to interface with the system tray of
59 * the desktop while the app is running. The <code>SystemTray</code>
60 * instance can be obtained from the {@link #getSystemTray} method.
61 * An application may not create its own instance of
62 * <code>SystemTray</code>.
63 *
64 * <p>The following code snippet demonstrates how to access
65 * and customize the system tray:
66 * <code>
67 * <pre>
68 * {@link TrayIcon} trayIcon = null;
69 * if (SystemTray.isSupported()) {
70 * // get the SystemTray instance
71 * SystemTray tray = SystemTray.{@link #getSystemTray};
72 * // load an image
73 * {@link java.awt.Image} image = {@link java.awt.Toolkit#getImage(String) Toolkit.getDefaultToolkit().getImage}(...);
74 * // create a action listener to listen for default action executed on the tray icon
75 * {@link java.awt.event.ActionListener} listener = new {@link java.awt.event.ActionListener ActionListener}() {
76 * public void {@link java.awt.event.ActionListener#actionPerformed actionPerformed}({@link java.awt.event.ActionEvent} e) {
77 * // execute default action of the application
78 * // ...
79 * }
80 * };
81 * // create a popup menu
82 * {@link java.awt.PopupMenu} popup = new {@link java.awt.PopupMenu#PopupMenu PopupMenu}();
83 * // create menu item for the default action
84 * MenuItem defaultItem = new MenuItem(...);
85 * defaultItem.addActionListener(listener);
86 * popup.add(defaultItem);
87 * /// ... add other items
92 * // ...
93 * // add the tray image
94 * try {
95 * tray.{@link SystemTray#add(TrayIcon) add}(trayIcon);
96 * } catch (AWTException e) {
97 * System.err.println(e);
98 * }
99 * // ...
100 * } else {
101 * // disable tray option in your application or
102 * // perform other actions
103 * ...
104 * }
105 * // ...
106 * // some time later
107 * // the application state has changed - update the image
108 * if (trayIcon != null) {
109 * trayIcon.{@link TrayIcon#setImage(java.awt.Image) setImage}(updatedImage);
110 * }
111 * // ...
112 * </pre>
113 * </code>
114 *
115 * @since 1.6
116 * @see TrayIcon
117 *
118 * @author Bino George
119 * @author Denis Mikhalkin
120 * @author Sharon Zakhour
121 * @author Anton Tarasov
122 */
123 public class SystemTray {
124 private static SystemTray systemTray;
125 private int currentIconID = 0; // each TrayIcon added gets a unique ID
126
127 transient private SystemTrayPeer peer;
128
129 private static final TrayIcon[] EMPTY_TRAY_ARRAY = new TrayIcon[0];
130
131 static {
132 AWTAccessor.setSystemTrayAccessor(
133 new AWTAccessor.SystemTrayAccessor() {
|
46 * be supported, in this case {@link SystemTray#getSystemTray()}
47 * throws {@link UnsupportedOperationException}. To detect whether the
48 * system tray is supported, use {@link SystemTray#isSupported}.
49 *
50 * <p>The <code>SystemTray</code> may contain one or more {@link
51 * TrayIcon TrayIcons}, which are added to the tray using the {@link
52 * #add} method, and removed when no longer needed, using the
53 * {@link #remove}. <code>TrayIcon</code> consists of an
54 * image, a popup menu and a set of associated listeners. Please see
55 * the {@link TrayIcon} class for details.
56 *
57 * <p>Every Java application has a single <code>SystemTray</code>
58 * instance that allows the app to interface with the system tray of
59 * the desktop while the app is running. The <code>SystemTray</code>
60 * instance can be obtained from the {@link #getSystemTray} method.
61 * An application may not create its own instance of
62 * <code>SystemTray</code>.
63 *
64 * <p>The following code snippet demonstrates how to access
65 * and customize the system tray:
66 * <pre>
67 * <code>
68 * {@link TrayIcon} trayIcon = null;
69 * if (SystemTray.isSupported()) {
70 * // get the SystemTray instance
71 * SystemTray tray = SystemTray.{@link #getSystemTray};
72 * // load an image
73 * {@link java.awt.Image} image = {@link java.awt.Toolkit#getImage(String) Toolkit.getDefaultToolkit().getImage}(...);
74 * // create a action listener to listen for default action executed on the tray icon
75 * {@link java.awt.event.ActionListener} listener = new {@link java.awt.event.ActionListener ActionListener}() {
76 * public void {@link java.awt.event.ActionListener#actionPerformed actionPerformed}({@link java.awt.event.ActionEvent} e) {
77 * // execute default action of the application
78 * // ...
79 * }
80 * };
81 * // create a popup menu
82 * {@link java.awt.PopupMenu} popup = new {@link java.awt.PopupMenu#PopupMenu PopupMenu}();
83 * // create menu item for the default action
84 * MenuItem defaultItem = new MenuItem(...);
85 * defaultItem.addActionListener(listener);
86 * popup.add(defaultItem);
87 * /// ... add other items
92 * // ...
93 * // add the tray image
94 * try {
95 * tray.{@link SystemTray#add(TrayIcon) add}(trayIcon);
96 * } catch (AWTException e) {
97 * System.err.println(e);
98 * }
99 * // ...
100 * } else {
101 * // disable tray option in your application or
102 * // perform other actions
103 * ...
104 * }
105 * // ...
106 * // some time later
107 * // the application state has changed - update the image
108 * if (trayIcon != null) {
109 * trayIcon.{@link TrayIcon#setImage(java.awt.Image) setImage}(updatedImage);
110 * }
111 * // ...
112 * </code>
113 * </pre>
114 *
115 * @since 1.6
116 * @see TrayIcon
117 *
118 * @author Bino George
119 * @author Denis Mikhalkin
120 * @author Sharon Zakhour
121 * @author Anton Tarasov
122 */
123 public class SystemTray {
124 private static SystemTray systemTray;
125 private int currentIconID = 0; // each TrayIcon added gets a unique ID
126
127 transient private SystemTrayPeer peer;
128
129 private static final TrayIcon[] EMPTY_TRAY_ARRAY = new TrayIcon[0];
130
131 static {
132 AWTAccessor.setSystemTrayAccessor(
133 new AWTAccessor.SystemTrayAccessor() {
|