Example usage for java.awt SystemTray getSystemTray

List of usage examples for java.awt SystemTray getSystemTray

Introduction

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

Prototype

public static SystemTray getSystemTray() 

Source Link

Document

Gets the SystemTray instance that represents the desktop's tray area.

Usage

From source file:edu.stanford.muse.launcher.Main.java

/** we need a system tray icon for management.
 * http://docs.oracle.com/javase/6/docs/api/java/awt/SystemTray.html */
public static void setupSystemTrayIcon() {
    // Set the app name in the menu bar for mac. 
    // c.f. http://stackoverflow.com/questions/8918826/java-os-x-lion-set-application-name-doesnt-work
    System.setProperty("apple.laf.useScreenMenuBar", "true");
    System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Muse");
    try {//  w w w  .j a va  2s  .com
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    TrayIcon trayIcon = null;
    if (SystemTray.isSupported()) {
        System.out.println("Adding Muse to the system tray");
        SystemTray tray = SystemTray.getSystemTray();

        URL u = Main.class.getClassLoader().getResource("muse-icon.png"); // note: this better be 16x16, Windows doesn't resize! Mac os does.
        System.out.println("muse icon resource is " + u);
        Image image = Toolkit.getDefaultToolkit().getImage(u);
        System.out.println("Image = " + image);

        // create menu items and their listeners
        ActionListener openMuseControlsListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    launchBrowser(BASE_URL + "/info");
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        };

        ActionListener QuitMuseListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                out.println("*** Received quit from system tray. Stopping the Jetty embedded web server.");
                try {
                    server.stop();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
                System.exit(0); // we need to explicitly system.exit because we now use Swing (due to system tray, etc).
            }
        };

        // create a popup menu
        PopupMenu popup = new PopupMenu();
        MenuItem defaultItem = new MenuItem("Open Muse window");
        defaultItem.addActionListener(openMuseControlsListener);
        popup.add(defaultItem);
        MenuItem quitItem = new MenuItem("Quit Muse");
        quitItem.addActionListener(QuitMuseListener);
        popup.add(quitItem);

        /// ... add other items
        // construct a TrayIcon
        String message = "Muse menu";
        // on windows - the tray menu is a little non-intuitive, needs a right click (plain click seems unused)
        if (System.getProperty("os.name").toLowerCase().indexOf("windows") >= 0)
            message = "Right click for Muse menu";
        trayIcon = new TrayIcon(image, message, popup);
        System.out.println("tray Icon = " + trayIcon);
        // set the TrayIcon properties
        //            trayIcon.addActionListener(openMuseControlsListener);
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            System.err.println(e);
        }
        // ...
    } else {
        // disable tray option in your application or
        // perform other actions
        //            ...
    }
    System.out.println("Done!");
    // ...
    // some time later
    // the application state has changed - update the image
    if (trayIcon != null) {
        //        trayIcon.setImage(updatedImage);
    }
    // ...
}

From source file:edu.stanford.epadd.launcher.Main.java

/** we need a system tray icon for management.
 * http://docs.oracle.com/javase/6/docs/api/java/awt/SystemTray.html */
public static void setupSystemTrayIcon() {
    // Set the app name in the menu bar for mac. 
    // c.f. http://stackoverflow.com/questions/8918826/java-os-x-lion-set-application-name-doesnt-work
    System.setProperty("apple.laf.useScreenMenuBar", "true");
    System.setProperty("com.apple.mrj.application.apple.menu.about.name", "ePADD");
    try {//from   w  w w . j  a v a2  s .c  o m
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    TrayIcon trayIcon = null;
    if (SystemTray.isSupported()) {
        System.out.println("Adding ePADD to the system tray");
        SystemTray tray = SystemTray.getSystemTray();

        URL u = Main.class.getClassLoader().getResource("muse-icon.png"); // note: this better be 16x16, Windows doesn't resize! Mac os does.
        System.out.println("ePADD icon resource is " + u);
        Image image = Toolkit.getDefaultToolkit().getImage(u);
        System.out.println("Image = " + image);

        // create menu items and their listeners
        ActionListener openMuseControlsListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    launchBrowser(BASE_URL); // no + "info" for epadd like in muse
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        };

        ActionListener QuitMuseListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                out.println("*** Received quit from system tray. Stopping the Jetty embedded web server.");
                try {
                    server.stop();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
                System.exit(0); // we need to explicitly system.exit because we now use Swing (due to system tray, etc).
            }
        };

        // create a popup menu
        PopupMenu popup = new PopupMenu();
        MenuItem defaultItem = new MenuItem("Open ePADD window");
        defaultItem.addActionListener(openMuseControlsListener);
        popup.add(defaultItem);
        MenuItem quitItem = new MenuItem("Quit ePADD");
        quitItem.addActionListener(QuitMuseListener);
        popup.add(quitItem);

        /// ... add other items
        // construct a TrayIcon
        String message = "ePADD menu";
        // on windows - the tray menu is a little non-intuitive, needs a right click (plain click seems unused)
        if (System.getProperty("os.name").toLowerCase().indexOf("windows") >= 0)
            message = "Right click for ePADD menu";
        trayIcon = new TrayIcon(image, message, popup);
        System.out.println("tray Icon = " + trayIcon);
        // set the TrayIcon properties
        //            trayIcon.addActionListener(openMuseControlsListener);
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            System.err.println(e);
        }
        // ...
    } else {
        // disable tray option in your application or
        // perform other actions
        //            ...
    }
    System.out.println("Done!");
    // ...
    // some time later
    // the application state has changed - update the image
    if (trayIcon != null) {
        //        trayIcon.setImage(updatedImage);
    }
    // ...
}

From source file:org.sleeksnap.ScreenSnapper.java

/**
 * Initialize the tray menu//from w  w w  . ja  va2s  .  c  o  m
 */
private void initializeTray() {
    // Add uploaders from the list we loaded earlier
    final PopupMenu tray = new PopupMenu();
    // Add the action menu
    tray.add(new ActionMenuItem(Language.getString("cropupload"), ScreenshotAction.CROP));
    tray.add(new ActionMenuItem(Language.getString("fullupload"), ScreenshotAction.FULL));

    if (Platform.isWindows() || Platform.isLinux()) {
        tray.add(new ActionMenuItem(Language.getString("activeupload"), ScreenshotAction.ACTIVE));
    }

    tray.addSeparator();

    tray.add(new ActionMenuItem(Language.getString("clipboardupload"), ScreenshotAction.CLIPBOARD));
    tray.add(new ActionMenuItem(Language.getString("fileupload"), ScreenshotAction.FILE));

    tray.addSeparator();

    final MenuItem settings = new MenuItem(Language.getString("options"));
    settings.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            if (!openSettings()) {
                icon.displayMessage(Language.getString("error"), Language.getString("optionsOpenError"),
                        TrayIcon.MessageType.ERROR);
            }
        }
    });
    tray.add(settings);

    final MenuItem exit = new MenuItem(Language.getString("exit"));
    exit.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            shutdown();
        }
    });
    tray.add(exit);

    icon = new TrayIcon(Toolkit.getDefaultToolkit().getImage(Resources.ICON),
            Application.NAME + " v" + Version.getVersionString());
    icon.setPopupMenu(tray);
    icon.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            if (lastUrl != null) {
                try {
                    Util.openURL(new URL(lastUrl));
                } catch (final Exception e1) {
                    showException(e1, "Unable to open URL");
                }
            }
        }
    });
    try {
        SystemTray.getSystemTray().add(icon);
    } catch (final AWTException e1) {
        this.showException(e1);
    }
}

From source file:edu.stanford.epadd.launcher.Splash.java

/** we need a system tray icon for management.
 * http://docs.oracle.com/javase/6/docs/api/java/awt/SystemTray.html */
public static void setupSystemTrayIcon() {
    // Set the app name in the menu bar for mac. 
    // c.f. http://stackoverflow.com/questions/8918826/java-os-x-lion-set-application-name-doesnt-work
    System.setProperty("apple.laf.useScreenMenuBar", "true");
    System.setProperty("com.apple.mrj.application.apple.menu.about.name", "ePADD");
    try {/*from   w ww.j av a2 s. c o  m*/
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    TrayIcon trayIcon = null;
    if (SystemTray.isSupported()) {
        tellUser("Adding ePADD to the system tray");
        SystemTray tray = SystemTray.getSystemTray();

        URL u = ePADD.class.getClassLoader().getResource("muse-icon.png"); // note: this better be 16x16, Windows doesn't resize! Mac os does.
        out.println("ePADD icon resource is " + u);

        Image image = Toolkit.getDefaultToolkit().getImage(u);
        out.println("Image = " + image);

        // create menu items and their listeners
        ActionListener openMuseControlsListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    launchBrowser(BASE_URL); // no + "info" for epadd like in muse
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        };

        ActionListener QuitMuseListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                out.println("*** Received quit from system tray. Stopping the Tomcat embedded web server.");
                try {
                    shutdownSessions();
                    server.stop();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
                System.exit(0); // we need to explicitly system.exit because we now use Swing (due to system tray, etc).
            }
        };

        // create a popup menu
        PopupMenu popup = new PopupMenu();
        MenuItem defaultItem = new MenuItem("Open ePADD window");
        defaultItem.addActionListener(openMuseControlsListener);
        popup.add(defaultItem);
        MenuItem quitItem = new MenuItem("Quit ePADD");
        quitItem.addActionListener(QuitMuseListener);
        popup.add(quitItem);

        /// ... add other items
        // construct a TrayIcon
        String message = "ePADD menu";
        // on windows - the tray menu is a little non-intuitive, needs a right click (plain click seems unused)
        if (System.getProperty("os.name").toLowerCase().indexOf("windows") >= 0)
            message = "Right click for ePADD menu";
        trayIcon = new TrayIcon(image, message, popup);
        System.out.println("tray Icon = " + trayIcon);
        // set the TrayIcon properties
        //            trayIcon.addActionListener(openMuseControlsListener);
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            out.println(e);
        }
        // ...
    } else {
        // disable tray option in your application or
        // perform other actions
        //            ...
    }
    System.out.println("Done!");
    // ...
    // some time later
    // the application state has changed - update the image
    if (trayIcon != null) {
        //        trayIcon.setImage(updatedImage);
    }
    // ...
}

From source file:org.languagetool.gui.Main.java

private void hideToTray() {
    if (!isInTray) {
        SystemTray tray = SystemTray.getSystemTray();
        String iconPath = tray.getTrayIconSize().height > 16 ? TRAY_ICON : TRAY_SMALL_ICON;
        URL iconUrl = JLanguageTool.getDataBroker().getFromResourceDirAsUrl(iconPath);
        Image img = Toolkit.getDefaultToolkit().getImage(iconUrl);
        PopupMenu popup = makePopupMenu();
        try {/*from w w w . j  a va 2  s.  c  om*/
            trayIcon = new TrayIcon(img, TRAY_TOOLTIP, popup);
            trayIcon.addMouseListener(new TrayActionListener());
            setTrayIcon();
            tray.add(trayIcon);
        } catch (AWTException e1) {
            Tools.showError(e1);
        }
    }
    isInTray = true;
    frame.setVisible(false);
}

From source file:edu.stanford.muse.launcher.Splash.java

/** we need a system tray icon for management.
 * http://docs.oracle.com/javase/6/docs/api/java/awt/SystemTray.html */
public static void setupSystemTrayIcon() {
    // Set the app name in the menu bar for mac. 
    // c.f. http://stackoverflow.com/questions/8918826/java-os-x-lion-set-application-name-doesnt-work
    System.setProperty("apple.laf.useScreenMenuBar", "true");
    System.setProperty("com.apple.mrj.application.apple.menu.about.name", "ePADD");
    try {//w  w w . j a va2s .c  o m
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    TrayIcon trayIcon = null;
    if (SystemTray.isSupported()) {
        System.out.println("Adding ePADD to the system tray");
        SystemTray tray = SystemTray.getSystemTray();

        URL u = TomcatMain.class.getClassLoader().getResource("muse-icon.png"); // note: this better be 16x16, Windows doesn't resize! Mac os does.
        System.out.println("ePADD icon resource is " + u);
        Image image = Toolkit.getDefaultToolkit().getImage(u);
        System.out.println("Image = " + image);

        // create menu items and their listeners
        ActionListener openMuseControlsListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    launchBrowser(BASE_URL + "/info", null);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        };

        ActionListener QuitMuseListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                out.println("*** Received quit from system tray. Stopping the Tomcat embedded web server.");
                try {
                    shutdownSessions();
                    server.stop();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
                System.exit(0); // we need to explicitly system.exit because we now use Swing (due to system tray, etc).
            }
        };

        // create a popup menu
        PopupMenu popup = new PopupMenu();
        MenuItem defaultItem = new MenuItem("Open ePADD window");
        defaultItem.addActionListener(openMuseControlsListener);
        popup.add(defaultItem);
        MenuItem quitItem = new MenuItem("Quit ePADD");
        quitItem.addActionListener(QuitMuseListener);
        popup.add(quitItem);

        /// ... add other items
        // construct a TrayIcon
        String message = "ePADD menu";
        // on windows - the tray menu is a little non-intuitive, needs a right click (plain click seems unused)
        if (System.getProperty("os.name").toLowerCase().indexOf("windows") >= 0)
            message = "Right click for ePADD menu";
        trayIcon = new TrayIcon(image, message, popup);
        System.out.println("tray Icon = " + trayIcon);
        // set the TrayIcon properties
        //            trayIcon.addActionListener(openMuseControlsListener);
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            err.println(e);
        }
        // ...
    } else {
        // disable tray option in your application or
        // perform other actions
        //            ...
    }
    System.out.println("Done!");
    // ...
    // some time later
    // the application state has changed - update the image
    if (trayIcon != null) {
        //        trayIcon.setImage(updatedImage);
    }
    // ...
}

From source file:org.languagetool.gui.Main.java

private void setTrayIcon() {
    if (trayIcon != null) {
        SystemTray tray = SystemTray.getSystemTray();
        boolean httpServerRunning = httpServer != null && httpServer.isRunning();
        boolean smallTray = tray.getTrayIconSize().height <= 16;
        String iconPath;/*from  w w w  .  j  a va 2 s .com*/
        if (httpServerRunning) {
            trayIcon.setToolTip(messages.getString("tray_tooltip_server_running"));
            iconPath = smallTray ? TRAY_SMALL_SERVER_ICON : TRAY_SERVER_ICON;
        } else {
            trayIcon.setToolTip(TRAY_TOOLTIP);
            iconPath = smallTray ? TRAY_SMALL_ICON : TRAY_ICON;
        }
        URL iconUrl = JLanguageTool.getDataBroker().getFromResourceDirAsUrl(iconPath);
        Image img = Toolkit.getDefaultToolkit().getImage(iconUrl);
        trayIcon.setImage(img);
    }
}

From source file:streamme.visuals.Main.java

public void loadTray() {
    if (SystemTray.isSupported()) {
        tray = SystemTray.getSystemTray();

        PopupMenu popup = new PopupMenu();
        MenuItem exitItem = new MenuItem("Exit");
        exitItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();/*from  www . j a  va2  s  .  c  om*/
            }
        });

        MenuItem openItem = new MenuItem("Open");
        openItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                minimizeToTray(false);
            }
        });

        popup.add(openItem);
        popup.add(exitItem);
        trayIcon = new TrayIcon(
                Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("res/icon16.png")),
                "StreamMe", popup);
        trayIcon.setImageAutoSize(true);
        trayIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent evt) {
                if (evt.getClickCount() == 2) {
                    //minimizeToTray(false);
                    return;
                }
                if (evt.getButton() == 1) {
                    minimizeToTray(!isMinimizedToTray());
                }
            }
        });

        addWindowStateListener(new WindowStateListener() {
            public void windowStateChanged(WindowEvent e) {
                if (e.getNewState() == ICONIFIED || e.getNewState() == 7) {
                    minimizeToTray(true);
                }
            }
        });

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                minimizeToTray(true);
            }
        });
        try {
            tray.add(trayIcon);
        } catch (AWTException ex) {
            System.out.println("Error adding icon to tray");
        }
    }
}

From source file:edu.stanford.epadd.launcher.Splash.java

/** we need a system tray icon for management.
 * http://docs.oracle.com/javase/6/docs/api/java/awt/SystemTray.html */
public static void setupSystemTrayIcon() {
    // Set the app name in the menu bar for mac. 
    // c.f. http://stackoverflow.com/questions/8918826/java-os-x-lion-set-application-name-doesnt-work
    System.setProperty("apple.laf.useScreenMenuBar", "true");
    System.setProperty("com.apple.mrj.application.apple.menu.about.name", "ePADD");
    try {//from w w w.j ava  2 s. c o m
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    TrayIcon trayIcon = null;
    if (SystemTray.isSupported()) {
        System.out.println("Adding ePADD to the system tray");
        SystemTray tray = SystemTray.getSystemTray();

        URL u = TomcatMain.class.getClassLoader().getResource("muse-icon.png"); // note: this better be 16x16, Windows doesn't resize! Mac os does.
        System.out.println("ePADD icon resource is " + u);
        Image image = Toolkit.getDefaultToolkit().getImage(u);
        System.out.println("Image = " + image);

        // create menu items and their listeners
        ActionListener openMuseControlsListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    launchBrowser(BASE_URL, null); // no + "info" for epadd like in muse
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        };

        ActionListener QuitMuseListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                out.println("*** Received quit from system tray. Stopping the Tomcat embedded web server.");
                try {
                    shutdownSessions();
                    server.stop();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
                System.exit(0); // we need to explicitly system.exit because we now use Swing (due to system tray, etc).
            }
        };

        // create a popup menu
        PopupMenu popup = new PopupMenu();
        MenuItem defaultItem = new MenuItem("Open ePADD window");
        defaultItem.addActionListener(openMuseControlsListener);
        popup.add(defaultItem);
        MenuItem quitItem = new MenuItem("Quit ePADD");
        quitItem.addActionListener(QuitMuseListener);
        popup.add(quitItem);

        /// ... add other items
        // construct a TrayIcon
        String message = "ePADD menu";
        // on windows - the tray menu is a little non-intuitive, needs a right click (plain click seems unused)
        if (System.getProperty("os.name").toLowerCase().indexOf("windows") >= 0)
            message = "Right click for ePADD menu";
        trayIcon = new TrayIcon(image, message, popup);
        System.out.println("tray Icon = " + trayIcon);
        // set the TrayIcon properties
        //            trayIcon.addActionListener(openMuseControlsListener);
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            out.println(e);
        }
        // ...
    } else {
        // disable tray option in your application or
        // perform other actions
        //            ...
    }
    System.out.println("Done!");
    // ...
    // some time later
    // the application state has changed - update the image
    if (trayIcon != null) {
        //        trayIcon.setImage(updatedImage);
    }
    // ...
}

From source file:Classes.MainForm.java

public void initTray() throws IOException {
    MainForm.trayIcon = null;//from ww  w .j ava2  s .com
    this.systemTray = SystemTray.getSystemTray();

    ActionListener actionListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                MainForm.trayIcon
                        .displayMessage(PropertiesHandler.getSingleton().getValue(1061),
                                PropertiesHandler.getSingleton().getValue(1098) + " "
                                        + PropertiesHandler.getSingleton().getValue(1099),
                                TrayIcon.MessageType.INFO);
            } catch (Exception e1) {
            }
        }
    };

    MouseListener mouseListener = new MouseListener() {

        public void mouseClicked(MouseEvent e) {
            if (SwingUtilities.isLeftMouseButton(e)) {
                if (!mainFrame.isVisible()) {
                    mainFrame.setVisible(true);
                }
            }
        }

        public void mouseEntered(MouseEvent e) {
            //mouseEntered not supported
            /*
            try {
            MainForm.trayIcon.displayMessage(PropertiesHandler.getSingleton().getValue(1061), PropertiesHandler.getSingleton().getValue(1098)+" "+PropertiesHandler.getSingleton().getValue(1099),TrayIcon.MessageType.INFO);
            } catch (Exception e1) {}
            */
        }

        public void mouseExited(MouseEvent e) {
        }//mouseExited not supported

        public void mousePressed(MouseEvent e) {
        }

        public void mouseReleased(MouseEvent e) {
        }
    };

    MainForm.trayIcon = new TrayIcon(trayIconIcon, PropertiesHandler.getSingleton().getValue(1061), popupMenu);//create tray icon
    MainForm.trayIcon.setImageAutoSize(true);
    MainForm.trayIcon.addActionListener(actionListener);
    MainForm.trayIcon.addMouseListener(mouseListener);

    try {
        systemTray.add(MainForm.trayIcon);
        MainForm.trayIcon.displayMessage(PropertiesHandler.getSingleton().getValue(1061),
                PropertiesHandler.getSingleton().getValue(1098) + " "
                        + PropertiesHandler.getSingleton().getValue(1099),
                TrayIcon.MessageType.INFO);
    } catch (Exception e1) {
    }
}