Example usage for java.awt PopupMenu add

List of usage examples for java.awt PopupMenu add

Introduction

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

Prototype

public MenuItem add(MenuItem mi) 

Source Link

Document

Adds the specified menu item to this menu.

Usage

From source file:com.ethercamp.harmony.desktop.HarmonyDesktop.java

private static void setTrayMenu(TrayIcon trayIcon, MenuItem... items) {
    if (!SystemTray.isSupported()) {
        log.error("System tray is not supported");
        return;/*from  w w w .j av  a 2  s  . c o  m*/
    }
    final SystemTray systemTray = SystemTray.getSystemTray();

    final PopupMenu popupMenu = new PopupMenu();
    stream(items).forEach(i -> popupMenu.add(i));
    trayIcon.setPopupMenu(popupMenu);

    try {
        stream(systemTray.getTrayIcons()).forEach(t -> systemTray.remove(t));
        systemTray.add(trayIcon);
    } catch (AWTException e) {
        log.error("Problem set tray", e);
    }
}

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

private static void createSysTray(final String url, final Server srv) {
    final TrayIcon trayIcon;
    try {//  www  . 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: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 {//from w w  w. ja  va  2  s  .  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 {// w  w  w.j  a v a2 s  .  co  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:misc.TrayIconDemo.java

private static void createAndShowGUI() {
    //Check the SystemTray support
    if (!SystemTray.isSupported()) {
        System.out.println("SystemTray is not supported");
        return;/*from   w  w w. j a v  a2s  .c o  m*/
    }
    final PopupMenu popup = new PopupMenu();
    final TrayIcon trayIcon = new TrayIcon(createImage("images/bulb.gif", "tray icon"));
    final SystemTray tray = SystemTray.getSystemTray();

    // Create a popup menu components
    MenuItem aboutItem = new MenuItem("About");
    CheckboxMenuItem cb1 = new CheckboxMenuItem("Set auto size");
    CheckboxMenuItem cb2 = new CheckboxMenuItem("Set tooltip");
    Menu displayMenu = new Menu("Display");
    MenuItem errorItem = new MenuItem("Error");
    MenuItem warningItem = new MenuItem("Warning");
    MenuItem infoItem = new MenuItem("Info");
    MenuItem noneItem = new MenuItem("None");
    MenuItem exitItem = new MenuItem("Exit");

    //Add components to popup menu
    popup.add(aboutItem);
    popup.addSeparator();
    popup.add(cb1);
    popup.add(cb2);
    popup.addSeparator();
    popup.add(displayMenu);
    displayMenu.add(errorItem);
    displayMenu.add(warningItem);
    displayMenu.add(infoItem);
    displayMenu.add(noneItem);
    popup.add(exitItem);

    trayIcon.setPopupMenu(popup);

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

    trayIcon.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "This dialog box is run from System Tray");
        }
    });

    aboutItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "This dialog box is run from the About menu item");
        }
    });

    cb1.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            int cb1Id = e.getStateChange();
            if (cb1Id == ItemEvent.SELECTED) {
                trayIcon.setImageAutoSize(true);
            } else {
                trayIcon.setImageAutoSize(false);
            }
        }
    });

    cb2.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            int cb2Id = e.getStateChange();
            if (cb2Id == ItemEvent.SELECTED) {
                trayIcon.setToolTip("Sun TrayIcon");
            } else {
                trayIcon.setToolTip(null);
            }
        }
    });

    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            MenuItem item = (MenuItem) e.getSource();
            //TrayIcon.MessageType type = null;
            System.out.println(item.getLabel());
            if ("Error".equals(item.getLabel())) {
                //type = TrayIcon.MessageType.ERROR;
                trayIcon.displayMessage("Sun TrayIcon Demo", "This is an error message",
                        TrayIcon.MessageType.ERROR);

            } else if ("Warning".equals(item.getLabel())) {
                //type = TrayIcon.MessageType.WARNING;
                trayIcon.displayMessage("Sun TrayIcon Demo", "This is a warning message",
                        TrayIcon.MessageType.WARNING);

            } else if ("Info".equals(item.getLabel())) {
                //type = TrayIcon.MessageType.INFO;
                trayIcon.displayMessage("Sun TrayIcon Demo", "This is an info message",
                        TrayIcon.MessageType.INFO);

            } else if ("None".equals(item.getLabel())) {
                //type = TrayIcon.MessageType.NONE;
                trayIcon.displayMessage("Sun TrayIcon Demo", "This is an ordinary message",
                        TrayIcon.MessageType.NONE);
            }
        }
    };

    errorItem.addActionListener(listener);
    warningItem.addActionListener(listener);
    infoItem.addActionListener(listener);
    noneItem.addActionListener(listener);

    exitItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            tray.remove(trayIcon);
            System.exit(0);
        }
    });
}

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 {/* www .jav  a2  s.  co 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.keyboardplaying.messaging.ui.ApplicationManager.java

private PopupMenu makeMenu() {
    // Create a pop-up menu components
    MenuItem exitItem = new MenuItem("Exit");
    exitItem.addActionListener(new ActionListener() {

        @Override/*from   w w  w .  j a va 2  s .  c  om*/
        public void actionPerformed(ActionEvent e) {
            context.close();
        }
    });

    // Add components to pop-up menu
    PopupMenu popup = new PopupMenu();
    popup.add(exitItem);
    return popup;
}

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 {// w ww .j  a  v a2s.  co  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:com.piusvelte.taplock.server.TapLockServer.java

private static void initialize() {
    (new File(APP_PATH)).mkdir();
    if (OS == OS_WIN)
        Security.addProvider(new BouncyCastleProvider());
    System.out.println("APP_PATH: " + APP_PATH);
    try {/*w ww .  j  a  va  2s  . co m*/
        sLogFileHandler = new FileHandler(sLog);
    } catch (SecurityException e) {
        writeLog("sLogFileHandler init: " + e.getMessage());
    } catch (IOException e) {
        writeLog("sLogFileHandler init: " + e.getMessage());
    }

    File propertiesFile = new File(sProperties);
    if (!propertiesFile.exists()) {
        try {
            propertiesFile.createNewFile();
        } catch (IOException e) {
            writeLog("propertiesFile.createNewFile: " + e.getMessage());
        }
    }

    Properties prop = new Properties();

    try {
        prop.load(new FileInputStream(sProperties));
        if (prop.isEmpty()) {
            prop.setProperty(sPassphraseKey, sPassphrase);
            prop.setProperty(sDisplaySystemTrayKey, Boolean.toString(sDisplaySystemTray));
            prop.setProperty(sDebuggingKey, Boolean.toString(sDebugging));
            prop.store(new FileOutputStream(sProperties), null);
        } else {
            if (prop.containsKey(sPassphraseKey))
                sPassphrase = prop.getProperty(sPassphraseKey);
            else
                prop.setProperty(sPassphraseKey, sPassphrase);
            if (prop.containsKey(sDisplaySystemTrayKey))
                sDisplaySystemTray = Boolean.parseBoolean(prop.getProperty(sDisplaySystemTrayKey));
            else
                prop.setProperty(sDisplaySystemTrayKey, Boolean.toString(sDisplaySystemTray));
            if (prop.containsKey(sDebuggingKey))
                sDebugging = Boolean.parseBoolean(prop.getProperty(sDebuggingKey));
            else
                prop.setProperty(sDebuggingKey, Boolean.toString(sDebugging));
        }
    } catch (FileNotFoundException e) {
        writeLog("prop load: " + e.getMessage());
    } catch (IOException e) {
        writeLog("prop load: " + e.getMessage());
    }

    if (sLogFileHandler != null) {
        sLogger = Logger.getLogger("TapLock");
        sLogger.setUseParentHandlers(false);
        sLogger.addHandler(sLogFileHandler);
        SimpleFormatter sf = new SimpleFormatter();
        sLogFileHandler.setFormatter(sf);
        writeLog("service starting");
    }

    if (sDisplaySystemTray && SystemTray.isSupported()) {
        final SystemTray systemTray = SystemTray.getSystemTray();
        Image trayIconImg = Toolkit.getDefaultToolkit()
                .getImage(TapLockServer.class.getResource("/systemtrayicon.png"));
        final TrayIcon trayIcon = new TrayIcon(trayIconImg, "Tap Lock");
        trayIcon.setImageAutoSize(true);
        PopupMenu popupMenu = new PopupMenu();
        MenuItem aboutItem = new MenuItem("About");
        CheckboxMenuItem toggleSystemTrayIcon = new CheckboxMenuItem("Display Icon in System Tray");
        toggleSystemTrayIcon.setState(sDisplaySystemTray);
        CheckboxMenuItem toggleDebugging = new CheckboxMenuItem("Debugging");
        toggleDebugging.setState(sDebugging);
        MenuItem shutdownItem = new MenuItem("Shutdown Tap Lock Server");
        popupMenu.add(aboutItem);
        popupMenu.add(toggleSystemTrayIcon);
        if (OS == OS_WIN) {
            MenuItem setPasswordItem = new MenuItem("Set password");
            popupMenu.add(setPasswordItem);
            setPasswordItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JPanel panel = new JPanel();
                    JLabel label = new JLabel("Enter your Windows account password:");
                    JPasswordField passField = new JPasswordField(32);
                    panel.add(label);
                    panel.add(passField);
                    String[] options = new String[] { "OK", "Cancel" };
                    int option = JOptionPane.showOptionDialog(null, panel, "Tap Lock", JOptionPane.NO_OPTION,
                            JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
                    if (option == 0) {
                        String password = encryptString(new String(passField.getPassword()));
                        if (password != null) {
                            Properties prop = new Properties();
                            try {
                                prop.load(new FileInputStream(sProperties));
                                prop.setProperty(sPasswordKey, password);
                                prop.store(new FileOutputStream(sProperties), null);
                            } catch (FileNotFoundException e1) {
                                writeLog("prop load: " + e1.getMessage());
                            } catch (IOException e1) {
                                writeLog("prop load: " + e1.getMessage());
                            }
                        }
                    }
                }
            });
        }
        popupMenu.add(toggleDebugging);
        popupMenu.add(shutdownItem);
        trayIcon.setPopupMenu(popupMenu);
        try {
            systemTray.add(trayIcon);
        } catch (AWTException e) {
            writeLog("systemTray.add: " + e.getMessage());
        }
        aboutItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String newline = System.getProperty("line.separator");
                newline += newline;
                JOptionPane.showMessageDialog(null, "Tap Lock" + newline + "Copyright (c) 2012 Bryan Emmanuel"
                        + newline
                        + "This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version."
                        + newline
                        + "This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details."
                        + newline
                        + "You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>."
                        + newline + "Bryan Emmanuel piusvelte@gmail.com");
            }
        });
        toggleSystemTrayIcon.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                setTrayIconDisplay(e.getStateChange() == ItemEvent.SELECTED);
                if (!sDisplaySystemTray)
                    systemTray.remove(trayIcon);
            }
        });
        toggleDebugging.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                setDebugging(e.getStateChange() == ItemEvent.SELECTED);
            }
        });
        shutdownItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                shutdown();
            }
        });
    }
    synchronized (sConnectionThreadLock) {
        (sConnectionThread = new ConnectionThread()).start();
    }
}

From source file:PopupDemo.java

void addPopup(Component c, String name) {
    PopupMenu pm = new PopupMenu();
    MenuItem mi = new MenuItem(name + "-1");
    mi.addActionListener(this);
    pm.add(mi);

    mi = new MenuItem(name + "-2");
    pm.add(mi);//ww  w .  j a  va  2s  . c  om

    setHash(c, pm);
    c.add(pm);
    c.addMouseListener(this);
}