Java TrayIcon.setImage(Image image)

Syntax

TrayIcon.setImage(Image image) has the following syntax.

public void setImage(Image image)

Example

In the following code shows how to use TrayIcon.setImage(Image image) method.


import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
// w w  w . j a  v  a 2 s.  co  m
public class Main {
  public static void main(String[] args)throws Exception {
    if (!SystemTray.isSupported()) {
      System.out.println("SystemTray is not supported");
      return;
    }

    SystemTray tray = SystemTray.getSystemTray();
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Image image = toolkit.getImage("trayIcon.jpg");

    PopupMenu menu = new PopupMenu();

    MenuItem messageItem = new MenuItem("Show Message");
    messageItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.out.println("asdf");
      }
    });
    menu.add(messageItem);

    MenuItem closeItem = new MenuItem("Close");
    closeItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.exit(0);
      }
    });
    menu.add(closeItem);
    TrayIcon icon = new TrayIcon(image, "SystemTray Demo", menu);
    icon.setImageAutoSize(true);

    image = toolkit.getImage("anotherImage.jpg");
    icon.setImage(image);
    tray.add(icon);
  }
}




















Home »
  Java Tutorial »
    java.awt »




BasicStroke
BorderLayout
CardLayout
Color
Cursor
Desktop
DesktopManager
DisplayMode
EventQueue
FlowLayout
FocusTraversalPolicy
Font
FontMetrics
GradientPaint
Graphics
Graphics2D
GraphicsConfiguration
GraphicsDevice
GraphicsEnvironment
GridBagConstraints
GridBagLayout
GridLayout
Image
ItemSelectable
KeyboardFocusManager
LayoutManager
LayoutManager2
Point
Rectangle
Robot
Shape
SplashScreen
SystemColor
SystemTray
TexturePaint
TrayIcon
Toolkit
Transparency