Example usage for java.awt SystemTray add

List of usage examples for java.awt SystemTray add

Introduction

In this page you can find the example usage for java.awt SystemTray add.

Prototype

public void add(TrayIcon trayIcon) throws AWTException 

Source Link

Document

Adds a TrayIcon to the SystemTray .

Usage

From source file:Main.java

public static void main(String[] a) throws Exception {
    if (SystemTray.isSupported()) {
        SystemTray tray = SystemTray.getSystemTray();

        trayIcon.setImageAutoSize(true);
        trayIcon.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("In here");
                trayIcon.displayMessage("Tester!", "Some action performed", TrayIcon.MessageType.INFO);
            }//from w ww  .  j  a  v  a  2  s. c o  m
        });

        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            System.err.println("TrayIcon could not be added.");
        }
    }
}

From source file:Main.java

License:asdf

public static void main(String[] args) throws Exception {
    if (!SystemTray.isSupported()) {
        System.out.println("SystemTray is not supported");
        return;/*from  w  w w  . j  a va 2 s.  com*/
    }

    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);

    tray.add(icon);
}

From source file:SystemTrayDemo.java

public static void main(String[] args) throws Exception {
    if (!SystemTray.isSupported()) {
        System.out.println("SystemTray is not supported");
        return;/*from   w  w w  .  ja  v  a 2s .  c  om*/
    }

    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) {
            JOptionPane.showMessageDialog(null, "www.java2s.com");
        }
    });
    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);

    tray.add(icon);
}

From source file:Main.java

License:asdf

public static void main(String[] args) throws Exception {
    if (!SystemTray.isSupported()) {
        System.out.println("SystemTray is not supported");
        return;//from w ww.ja  v  a  2  s .c  o m
    }

    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);

    icon.setToolTip("tooltip");
    tray.add(icon);
}

From source file:Main.java

License:asdf

public static void main(String[] args) throws Exception {
    if (!SystemTray.isSupported()) {
        System.out.println("SystemTray is not supported");
        return;// w  ww .j  a v  a2 s  .  c o  m
    }

    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);
}

From source file:SystemTrayTest.java

public static void main(String[] args) {
    final TrayIcon trayIcon;

    if (!SystemTray.isSupported()) {
        System.err.println("System tray is not supported.");
        return;// w  w w. j  ava 2  s .c om
    }

    SystemTray tray = SystemTray.getSystemTray();
    Image image = Toolkit.getDefaultToolkit().getImage("cookie.png");

    PopupMenu popup = new PopupMenu();
    MenuItem exitItem = new MenuItem("Exit");
    exitItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    popup.add(exitItem);

    trayIcon = new TrayIcon(image, "Your Fortune", popup);

    trayIcon.setImageAutoSize(true);
    trayIcon.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            trayIcon.displayMessage("How do I turn this off?",
                    "Right-click on the fortune cookie and select Exit.", TrayIcon.MessageType.INFO);
        }
    });

    try {
        tray.add(trayIcon);
    } catch (AWTException e) {
        System.err.println("TrayIcon could not be added.");
        return;
    }

    final List<String> fortunes = readFortunes();
    Timer timer = new Timer(10000, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int index = (int) (fortunes.size() * Math.random());
            trayIcon.displayMessage("Your Fortune", fortunes.get(index), TrayIcon.MessageType.INFO);
        }
    });
    timer.start();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    if (!SystemTray.isSupported()) {
        return;/*w ww  . j a  v a2  s  .  co m*/
    }
    SystemTray tray = SystemTray.getSystemTray();

    PropertyChangeListener pcl;
    pcl = new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent pce) {
            System.out.println("Property changed = " + pce.getPropertyName());
            TrayIcon[] tia = (TrayIcon[]) pce.getOldValue();
            if (tia != null) {
                for (int i = 0; i < tia.length; i++)
                    System.out.println(tia[i]);
            }

            tia = (TrayIcon[]) pce.getNewValue();
            if (tia != null) {
                for (int i = 0; i < tia.length; i++)
                    System.out.println(tia[i]);
            }
        }
    };
    tray.addPropertyChangeListener("trayIcons", pcl);

    Dimension size = tray.getTrayIconSize();
    BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();

    g.setColor(Color.blue);
    g.fillRect(0, 0, size.width, size.height);
    TrayIcon icon = null;
    tray.add(icon = new TrayIcon(bi));

    Thread.sleep(3000);
    tray.remove(icon);

    Thread.sleep(3000);
    System.exit(0);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    if (!SystemTray.isSupported()) {
        return;//  w  w w  .java2 s . c om
    }
    SystemTray tray = SystemTray.getSystemTray();

    PropertyChangeListener pcl;
    pcl = new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent pce) {
            System.out.println("Property changed = " + pce.getPropertyName());
            TrayIcon[] tia = (TrayIcon[]) pce.getOldValue();
            if (tia != null) {
                for (int i = 0; i < tia.length; i++)
                    System.out.println(tia[i]);
            }

            tia = (TrayIcon[]) pce.getNewValue();
            if (tia != null) {
                for (int i = 0; i < tia.length; i++)
                    System.out.println(tia[i]);
            }
        }
    };
    tray.addPropertyChangeListener("trayIcons", pcl);

    Dimension size = tray.getTrayIconSize();

    BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();

    g.setColor(Color.blue);
    g.fillRect(0, 0, size.width, size.height);
    TrayIcon icon = null;
    tray.add(icon = new TrayIcon(bi));

    Thread.sleep(3000);
    tray.remove(icon);

    tray.removePropertyChangeListener("trayIcons", pcl);

    Thread.sleep(3000);
    System.exit(0);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    if (!SystemTray.isSupported()) {
        return;/* w w  w.ja v  a  2s . c  om*/
    }
    SystemTray tray = SystemTray.getSystemTray();

    PropertyChangeListener pcl;
    pcl = new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent pce) {
            System.out.println("Property changed = " + pce.getPropertyName());
            TrayIcon[] tia = (TrayIcon[]) pce.getOldValue();
            if (tia != null) {
                for (int i = 0; i < tia.length; i++)
                    System.out.println(tia[i]);
            }

            tia = (TrayIcon[]) pce.getNewValue();
            if (tia != null) {
                for (int i = 0; i < tia.length; i++)
                    System.out.println(tia[i]);
            }
        }
    };
    tray.addPropertyChangeListener("trayIcons", pcl);

    Dimension size = tray.getTrayIconSize();

    TrayIcon[] icons = tray.getTrayIcons();

    BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();

    g.setColor(Color.blue);
    g.fillRect(0, 0, size.width, size.height);
    TrayIcon icon = null;
    tray.add(icon = new TrayIcon(bi));

    Thread.sleep(3000);
    tray.remove(icon);

    Thread.sleep(3000);
    System.exit(0);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    if (!SystemTray.isSupported()) {
        return;/*from   w w w.  j  a v  a 2 s.c  o m*/
    }
    SystemTray tray = SystemTray.getSystemTray();

    PropertyChangeListener pcl;
    pcl = new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent pce) {
            System.out.println("Property changed = " + pce.getPropertyName());
            TrayIcon[] tia = (TrayIcon[]) pce.getOldValue();
            if (tia != null) {
                for (int i = 0; i < tia.length; i++)
                    System.out.println(tia[i]);
            }

            tia = (TrayIcon[]) pce.getNewValue();
            if (tia != null) {
                for (int i = 0; i < tia.length; i++)
                    System.out.println(tia[i]);
            }
        }
    };
    tray.addPropertyChangeListener("trayIcons", pcl);
    PropertyChangeListener[] listeners = tray.getPropertyChangeListeners("trayIcons");

    Dimension size = tray.getTrayIconSize();
    BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();

    g.setColor(Color.blue);
    g.fillRect(0, 0, size.width, size.height);
    TrayIcon icon = null;
    tray.add(icon = new TrayIcon(bi));

    Thread.sleep(3000);
    tray.remove(icon);

    Thread.sleep(3000);
    System.exit(0);
}