Save This Page
Home » openjdk-7 » java » awt » [javadoc | source]
java.awt
public class: TrayIcon [javadoc | source]
java.lang.Object
   java.awt.TrayIcon
A TrayIcon object represents a tray icon that can be added to the system tray . A TrayIcon can have a tooltip (text), an image, a popup menu, and a set of listeners associated with it.

A TrayIcon can generate various MouseEvents and supports adding corresponding listeners to receive notification of these events. TrayIcon processes some of the events by itself. For example, by default, when the right-mouse click is performed on the TrayIcon it displays the specified popup menu. When the mouse hovers over the TrayIcon the tooltip is displayed.

Note: When the MouseEvent is dispatched to its registered listeners its component property will be set to null. (See java.awt.event.ComponentEvent#getComponent ) The source property will be set to this TrayIcon. (See java.util.EventObject#getSource )

Note: A well-behaved TrayIcon implementation will assign different gestures to showing a popup menu and selecting a tray icon.

A TrayIcon can generate an ActionEvent . On some platforms, this occurs when the user selects the tray icon using either the mouse or keyboard.

If a SecurityManager is installed, the AWTPermission {@code accessSystemTray} must be granted in order to create a {@code TrayIcon}. Otherwise the constructor will throw a SecurityException.

See the SystemTray class overview for an example on how to use the TrayIcon API.

Nested Class Summary:
public enum class  TrayIcon.MessageType  The message type determines which icon will be displayed in the caption of the message, and a possible system sound a message may generate upon showing. 
Field Summary
transient  MouseListener mouseListener     
transient  MouseMotionListener mouseMotionListener     
transient  ActionListener actionListener     
Constructor:
 public TrayIcon(Image image) 
    Creates a TrayIcon with the specified image.
    Parameters:
    image - the Image to be used
    Throws:
    IllegalArgumentException - if image is null
    UnsupportedOperationException - if the system tray isn't supported by the current platform
    HeadlessException - if {@code GraphicsEnvironment.isHeadless()} returns {@code true}
    SecurityException - if {@code accessSystemTray} permission is not granted
    Also see:
    SystemTray#add(TrayIcon)
    TrayIcon#TrayIcon(Image, String, PopupMenu)
    TrayIcon#TrayIcon(Image, String)
    SecurityManager#checkPermission
    AWTPermission
 public TrayIcon(Image image,
    String tooltip) 
    Creates a TrayIcon with the specified image and tooltip text.
    Parameters:
    image - the Image to be used
    tooltip - the string to be used as tooltip text; if the value is null no tooltip is shown
    Throws:
    IllegalArgumentException - if image is null
    UnsupportedOperationException - if the system tray isn't supported by the current platform
    HeadlessException - if {@code GraphicsEnvironment.isHeadless()} returns {@code true}
    SecurityException - if {@code accessSystemTray} permission is not granted
    Also see:
    SystemTray#add(TrayIcon)
    TrayIcon#TrayIcon(Image)
    TrayIcon#TrayIcon(Image, String, PopupMenu)
    SecurityManager#checkPermission
    AWTPermission
 public TrayIcon(Image image,
    String tooltip,
    PopupMenu popup) 
    Creates a TrayIcon with the specified image, tooltip and popup menu.
    Parameters:
    image - the Image to be used
    tooltip - the string to be used as tooltip text; if the value is null no tooltip is shown
    popup - the menu to be used for the tray icon's popup menu; if the value is null no popup menu is shown
    Throws:
    IllegalArgumentException - if image is null
    UnsupportedOperationException - if the system tray isn't supported by the current platform
    HeadlessException - if {@code GraphicsEnvironment.isHeadless()} returns {@code true}
    SecurityException - if {@code accessSystemTray} permission is not granted
    Also see:
    SystemTray#add(TrayIcon)
    TrayIcon#TrayIcon(Image, String)
    TrayIcon#TrayIcon(Image)
    PopupMenu
    MouseListener
    addMouseListener(MouseListener)
    SecurityManager#checkPermission
    AWTPermission
Method from java.awt.TrayIcon Summary:
addActionListener,   addMouseListener,   addMouseMotionListener,   addNotify,   dispatchEvent,   displayMessage,   getAccessControlContext,   getActionCommand,   getActionListeners,   getID,   getImage,   getMouseListeners,   getMouseMotionListeners,   getPopupMenu,   getSize,   getToolTip,   isImageAutoSize,   processActionEvent,   processEvent,   processMouseEvent,   processMouseMotionEvent,   removeActionListener,   removeMouseListener,   removeMouseMotionListener,   removeNotify,   setActionCommand,   setID,   setImage,   setImageAutoSize,   setPopupMenu,   setToolTip
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from java.awt.TrayIcon Detail:
 public synchronized  void addActionListener(ActionListener listener) 
    Adds the specified action listener to receive ActionEvents from this TrayIcon. Action events usually occur when a user selects the tray icon, using either the mouse or keyboard. The conditions in which action events are generated are platform-dependent.

    Calling this method with a null value has no effect.

    Refer to AWT Threading Issues for details on AWT's threading model.

 public synchronized  void addMouseListener(MouseListener listener) 
    Adds the specified mouse listener to receive mouse events from this TrayIcon. Calling this method with a null value has no effect.

    Note: The {@code MouseEvent}'s coordinates (received from the {@code TrayIcon}) are relative to the screen, not the {@code TrayIcon}.

    Note: The MOUSE_ENTERED and MOUSE_EXITED mouse events are not supported.

    Refer to AWT Threading Issues for details on AWT's threading model.

 public synchronized  void addMouseMotionListener(MouseMotionListener listener) 
    Adds the specified mouse listener to receive mouse-motion events from this TrayIcon. Calling this method with a null value has no effect.

    Note: The {@code MouseEvent}'s coordinates (received from the {@code TrayIcon}) are relative to the screen, not the {@code TrayIcon}.

    Note: The MOUSE_DRAGGED mouse event is not supported.

    Refer to AWT Threading Issues for details on AWT's threading model.

  void addNotify() throws AWTException 
  void dispatchEvent(AWTEvent e) 
 public  void displayMessage(String caption,
    String text,
    MessageType messageType) 
    Displays a popup message near the tray icon. The message will disappear after a time or if the user clicks on it. Clicking on the message may trigger an {@code ActionEvent}.

    Either the caption or the text may be null, but an NullPointerException is thrown if both are null. When displayed, the caption or text strings may be truncated on some platforms; the number of characters that may be displayed is platform-dependent.

    Note: Some platforms may not support showing a message.

 final AccessControlContext getAccessControlContext() 
 public String getActionCommand() 
    Returns the command name of the action event fired by this tray icon.
 public synchronized ActionListener[] getActionListeners() 
    Returns an array of all the action listeners registered on this TrayIcon.
 int getID() 
 public Image getImage() 
    Returns the current image used for this TrayIcon.
 public synchronized MouseListener[] getMouseListeners() 
    Returns an array of all the mouse listeners registered on this TrayIcon.
 public synchronized MouseMotionListener[] getMouseMotionListeners() 
    Returns an array of all the mouse-motion listeners registered on this TrayIcon.
 public PopupMenu getPopupMenu() 
    Returns the popup menu associated with this TrayIcon.
 public Dimension getSize() 
    Returns the size, in pixels, of the space that the tray icon occupies in the system tray. For the tray icon that is not yet added to the system tray, the returned size is equal to the result of the SystemTray#getTrayIconSize .
 public String getToolTip() 
    Returns the tooltip string associated with this TrayIcon.
 public boolean isImageAutoSize() 
    Returns the value of the auto-size property.
  void processActionEvent(ActionEvent e) 
  void processEvent(AWTEvent e) 
  void processMouseEvent(MouseEvent e) 
  void processMouseMotionEvent(MouseEvent e) 
 public synchronized  void removeActionListener(ActionListener listener) 
    Removes the specified action listener. Calling this method with null or an invalid value has no effect.

    Refer to AWT Threading Issues for details on AWT's threading model.

 public synchronized  void removeMouseListener(MouseListener listener) 
    Removes the specified mouse listener. Calling this method with null or an invalid value has no effect.

    Refer to AWT Threading Issues for details on AWT's threading model.

 public synchronized  void removeMouseMotionListener(MouseMotionListener listener) 
    Removes the specified mouse-motion listener. Calling this method with null or an invalid value has no effect.

    Refer to AWT Threading Issues for details on AWT's threading model.

  void removeNotify() 
 public  void setActionCommand(String command) 
    Sets the command name for the action event fired by this tray icon. By default, this action command is set to null.
  void setID(int id) 
 public  void setImage(Image image) 
    Sets the image for this TrayIcon. The previous tray icon image is discarded without calling the java.awt.Image#flush method — you will need to call it manually.

    If the image represents an animated image, it will be animated automatically.

    See the #setImageAutoSize(boolean) property for details on the size of the displayed image.

    Calling this method with the same image that is currently being used has no effect.

 public  void setImageAutoSize(boolean autosize) 
    Sets the auto-size property. Auto-size determines whether the tray image is automatically sized to fit the space allocated for the image on the tray. By default, the auto-size property is set to false.

    If auto-size is false, and the image size doesn't match the tray icon space, the image is painted as-is inside that space — if larger than the allocated space, it will be cropped.

    If auto-size is true, the image is stretched or shrunk to fit the tray icon space.

 public  void setPopupMenu(PopupMenu popup) 
    Sets the popup menu for this TrayIcon. If popup is null, no popup menu will be associated with this TrayIcon.

    Note that this popup must not be added to any parent before or after it is set on the tray icon. If you add it to some parent, the popup may be removed from that parent.

    The {@code popup} can be set on one {@code TrayIcon} only. Setting the same popup on multiple {@code TrayIcon}s will cause an {@code IllegalArgumentException}.

    Note: Some platforms may not support showing the user-specified popup menu component when the user right-clicks the tray icon. In this situation, either no menu will be displayed or, on some systems, a native version of the menu may be displayed.

 public  void setToolTip(String tooltip) 
    Sets the tooltip string for this TrayIcon. The tooltip is displayed automatically when the mouse hovers over the icon. Setting the tooltip to null removes any tooltip text. When displayed, the tooltip string may be truncated on some platforms; the number of characters that may be displayed is platform-dependent.