- java.lang.Object
-
- java.awt.SystemTray
-
public class SystemTray extends Object
TheSystemTray
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
-
-
Method Summary
Methods Modifier and Type Method and Description void
add(TrayIcon trayIcon)
Adds aTrayIcon
to theSystemTray
.void
addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
Adds aPropertyChangeListener
to the list of listeners for the specific property.PropertyChangeListener[]
getPropertyChangeListeners(String propertyName)
Returns an array of all the listeners that have been associated with the named property.static SystemTray
getSystemTray()
Gets theSystemTray
instance that represents the desktop's tray area.TrayIcon[]
getTrayIcons()
Returns an array of all icons added to the tray by this application.Dimension
getTrayIconSize()
Returns the size, in pixels, of the space that a tray icon will occupy in the system tray.static boolean
isSupported()
Returns whether the system tray is supported on the current platform.void
remove(TrayIcon trayIcon)
Removes the specifiedTrayIcon
from theSystemTray
.void
removePropertyChangeListener(String propertyName, PropertyChangeListener listener)
Removes aPropertyChangeListener
from the listener list for a specific property.
-
-
-
Method Detail
-
getSystemTray
public static SystemTray getSystemTray()
Gets theSystemTray
instance that represents the desktop's tray area. This always returns the same instance per application. On some platforms the system tray may not be supported. You may use theisSupported()
method to check if the system tray is supported.If a SecurityManager is installed, the AWTPermission
accessSystemTray
must be granted in order to get theSystemTray
instance. Otherwise this method will throw a SecurityException.- Returns:
- the
SystemTray
instance that represents the desktop's tray area - Throws:
UnsupportedOperationException
- if the system tray isn't supported by the current platformHeadlessException
- ifGraphicsEnvironment.isHeadless()
returnstrue
SecurityException
- ifaccessSystemTray
permission is not granted- See Also:
add(TrayIcon)
,TrayIcon
,isSupported()
,SecurityManager.checkPermission(java.security.Permission)
,AWTPermission
-
isSupported
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 (seeTrayIcon.setPopupMenu(PopupMenu)
) or an action event (seeTrayIcon.addActionListener(ActionListener)
).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
example
for an example of how to do this.Note: When implementing
SystemTray
andTrayIcon
it is strongly recommended that you assign different gestures to the popup menu and an action event. Overloading a gesture for both purposes is confusing and may prevent the user from accessing one or the other.- Returns:
false
if no system tray access is supported; this method returnstrue
if the minimal system tray access is supported but does not guarantee that all system tray functionality is supported for the current platform- See Also:
getSystemTray()
-
add
public void add(TrayIcon trayIcon) throws AWTException
Adds aTrayIcon
to theSystemTray
. The tray icon becomes visible in the system tray once it is added. The order in which icons are displayed in a tray is not specified - it is platform and implementation-dependent.All icons added by the application are automatically removed from the
SystemTray
upon application exit and also when the desktop system tray becomes unavailable.- Parameters:
trayIcon
- theTrayIcon
to be added- Throws:
NullPointerException
- iftrayIcon
isnull
IllegalArgumentException
- if the same instance of aTrayIcon
is added more than onceAWTException
- if the desktop system tray is missing- See Also:
remove(TrayIcon)
,getSystemTray()
,TrayIcon
,Image
-
remove
public void remove(TrayIcon trayIcon)
Removes the specifiedTrayIcon
from theSystemTray
.All icons added by the application are automatically removed from the
SystemTray
upon application exit and also when the desktop system tray becomes unavailable.If
trayIcon
isnull
or was not added to the system tray, no exception is thrown and no action is performed.- Parameters:
trayIcon
- theTrayIcon
to be removed- See Also:
add(TrayIcon)
,TrayIcon
-
getTrayIcons
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. Some browsers partition applets in different code bases into separate contexts, and establish walls between these contexts. In such a scenario, only the tray icons added from this context will be returned.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
TrayIcon
from theSystemTray
, use theremove(TrayIcon)
method.- Returns:
- an array of all tray icons added to this tray, or an empty array if none has been added
- See Also:
add(TrayIcon)
,TrayIcon
-
getTrayIconSize
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 methods to acquire the preferred size for the image property of a tray icon before it is created. For convenience, there is a similar methodTrayIcon.getSize()
in theTrayIcon
class.- Returns:
- the default size of a tray icon, in pixels
- See Also:
TrayIcon.setImageAutoSize(boolean)
,Image
,TrayIcon.getSize()
-
addPropertyChangeListener
public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
Adds aPropertyChangeListener
to the list of listeners for the specific property. The following properties are currently supported:Property Description trayIcons
The SystemTray
's array ofTrayIcon
objects. The array is accessed via thegetTrayIcons()
method.
This property is changed when a tray icon is added to (or removed from) the system tray.
For example, this property is changed when the system tray becomes unavailable on the desktop
and the tray icons are automatically removed.systemTray
This property contains SystemTray
instance when the system tray is available ornull
otherwise.
This property is changed when the system tray becomes available or unavailable on the desktop.
The property is accessed by thegetSystemTray()
method.listener
listens to property changes only in this context.If
listener
isnull
, no exception is thrown and no action is performed.- Parameters:
propertyName
- the specified propertylistener
- the property change listener to be added- See Also:
removePropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)
,getPropertyChangeListeners(java.lang.String)
-
removePropertyChangeListener
public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener)
Removes aPropertyChangeListener
from the listener list for a specific property.The
PropertyChangeListener
must be from this context.If
propertyName
orlistener
isnull
or invalid, no exception is thrown and no action is taken.- Parameters:
propertyName
- the specified propertylistener
- the PropertyChangeListener to be removed- See Also:
addPropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)
,getPropertyChangeListeners(java.lang.String)
-
getPropertyChangeListeners
public PropertyChangeListener[] getPropertyChangeListeners(String propertyName)
Returns an array of all the listeners that have been associated with the named property.Only the listeners in this context are returned.
- Parameters:
propertyName
- the specified property- Returns:
- all of the
PropertyChangeListener
s associated with the named property; if no such listeners have been added or ifpropertyName
isnull
or invalid, an empty array is returned - See Also:
addPropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)
,removePropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)
-
-
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 11/06/2005 gemaakt, de laatste keer de 04/03/2020 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/java-api-rf-java/awt/systemtray.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.
Referenties
Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.