Example usage for java.awt TrayIcon addActionListener

List of usage examples for java.awt TrayIcon addActionListener

Introduction

In this page you can find the example usage for java.awt TrayIcon addActionListener.

Prototype

public synchronized void addActionListener(ActionListener listener) 

Source Link

Document

Adds the specified action listener to receive ActionEvent s from this TrayIcon .

Usage

From source file:org.duracloud.syncui.SyncUIDriver.java

private static void createSysTray(final String url, final Server srv) {
    final TrayIcon trayIcon;
    try {/* w w w . j a  v a  2  s . c o m*/

        if (SystemTray.isSupported()) {

            SystemTray tray = SystemTray.getSystemTray();
            InputStream is = org.duracloud.syncui.SyncUIDriver.class.getClassLoader()
                    .getResourceAsStream("tray.png");

            Image image = ImageIO.read(is);
            MouseListener mouseListener = new MouseListener() {

                public void mouseClicked(MouseEvent e) {
                    log.debug("Tray Icon - Mouse clicked!");
                }

                public void mouseEntered(MouseEvent e) {
                    log.debug("Tray Icon - Mouse entered!");
                }

                public void mouseExited(MouseEvent e) {
                    log.debug("Tray Icon - Mouse exited!");
                }

                public void mousePressed(MouseEvent e) {
                    log.debug("Tray Icon - Mouse pressed!");
                }

                public void mouseReleased(MouseEvent e) {
                    log.debug("Tray Icon - Mouse released!");
                }
            };

            ActionListener exitListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    log.info("Exiting...");
                    try {
                        srv.stop();
                        while (!srv.isStopped()) {
                            WaitUtil.wait(1);
                        }
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }

                    System.exit(0);
                }
            };

            PopupMenu popup = new PopupMenu();
            MenuItem view = new MenuItem("View Status");
            view.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent event) {
                    launchBrowser(url);
                }
            });
            popup.add(view);

            MenuItem exit = new MenuItem("Exit");
            exit.addActionListener(exitListener);
            popup.add(exit);

            trayIcon = new TrayIcon(image, "DuraCloud Sync Tool", popup);

            ActionListener actionListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    trayIcon.displayMessage("Action Event", "An Action Event Has Been Performed!",
                            TrayIcon.MessageType.INFO);
                }
            };

            trayIcon.setImageAutoSize(true);
            trayIcon.addActionListener(actionListener);
            trayIcon.addMouseListener(mouseListener);

            try {
                tray.add(trayIcon);
            } catch (AWTException e) {
                log.error("TrayIcon could not be added.");
            }
        } else {
            log.warn("System Tray is not supported.");
        }

    } catch (Exception ex) {
        log.error(ex.getMessage(), ex);
    }

}

From source file:org.notebook.gui.MainFrame.java

private void installTrayIcon() {
    visibleTrayIcon = false;//  w  w  w  . ja va  2  s  .c o  m
    try {
        if (java.awt.SystemTray.isSupported()) {// ????
            java.awt.SystemTray st = java.awt.SystemTray.getSystemTray();
            java.awt.TrayIcon ti = new java.awt.TrayIcon(appIcon16());
            ti.setToolTip(this.getTitle());
            ti.setPopupMenu(menu.getTrayMenu());
            ti.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    events.fireEvent(MenuToolbar.SHOWWINDOW, mainFrame);
                }
            });
            st.add(ti);
            visibleTrayIcon = true;
        }
    } catch (Exception e) {
        log.error(e, e);
    }
}