Example usage for java.awt PopupMenu PopupMenu

List of usage examples for java.awt PopupMenu PopupMenu

Introduction

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

Prototype

public PopupMenu() throws HeadlessException 

Source Link

Document

Creates a new popup menu with an empty name.

Usage

From source file:iqq.app.ui.manager.MainManager.java

public void enableTray() {
    if (SystemTray.isSupported() && tray == null) {
        menu = new PopupMenu();
        MenuItem restore = new MenuItem("  ?  ");
        restore.addActionListener(new ActionListener() {
            @Override/*from   w  ww.  ja v a  2s  . c  om*/
            public void actionPerformed(ActionEvent e) {
                show();
            }
        });
        MenuItem exit = new MenuItem("  ?  ");
        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        menu.add(restore);
        menu.addSeparator();
        menu.add(exit);

        if (SystemUtils.isMac()) {
            defaultImage = skinService.getIconByKey("window/titleWIconBlack").getImage();
        } else {
            defaultImage = mainFrame.getIconImage();
        }
        blankImage = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
        tray = SystemTray.getSystemTray();
        icon = new TrayIcon(defaultImage, "IQQ");
        icon.setImageAutoSize(true);
        if (!SystemUtils.isMac()) {
            icon.setPopupMenu(menu);
        }
        try {
            tray.add(icon);
            icon.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    logger.debug("MouseEvent " + e.getButton() + " " + e.getClickCount());
                    //??
                    if (e.getButton() == MouseEvent.BUTTON1) {
                        if (flashOwner != null) {
                            Map<String, Object> data = (Map<String, Object>) flashOwner.getAttachment();
                            if (data != null && data.containsKey("action")
                                    && data.get("action").equals("ADD_BUDDY_REQUEST")) {
                                IMContext.getBean(FrameManager.class).showGetFriendRequest((IMBuddy) flashOwner,
                                        data.get("buddy_request_id").toString());
                                eventService.broadcast(new UIEvent(UIEventType.FLASH_USER_STOP, flashOwner));
                                return;
                            } else {
                                // ?
                                chatManager.addChat(flashOwner);
                            }
                        } else {
                            show();
                        }
                    }
                }
            });
        } catch (AWTException e) {
            logger.error("SystemTray add icon.", e);
        }
    }

}

From source file:zxmax.tools.timerreview.Main.java

private TrayIcon getTrayIcon() throws AWTException {
    final TrayIcon trayIcon = new TrayIcon(
            createImage("images/bulb.gif", I18N.getLabel(getClass(), "tray.icon.description")));

    final SystemTray tray = SystemTray.getSystemTray();
    tray.add(trayIcon);//from   w w w  . j av  a 2s.com

    final PopupMenu popup = new PopupMenu();
    trayIcon.setPopupMenu(popup);

    /*
     * Serve per aprire il popup sulla tray icon con il tasto sx del mouse.
     * Il problema  chiuderla/nasconderla ...
     */
    // trayIcon.addMouseListener(new TrayIconMouseAdapter(popup));

    final MenuItem exitItem = new MenuItem(I18N.getLabel(getClass(), "exit"));
    final MenuItem newTimerItem = new MenuItem(I18N.getLabel(getClass(), "new.timer"));
    newTimerItem.setEnabled(false);
    final MenuItem infoItem = new MenuItem(I18N.getLabel(getClass(), "info"));
    popup.add(exitItem);
    popup.add(newTimerItem);
    popup.add(infoItem);

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

    newTimerItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            StartTimerWindow timer = new StartTimerWindow();
            timer.setVisible(true);
        }
    });

    infoItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            InfoWindow infoWindow = new InfoWindow();
            infoWindow.setVisible(true);
            infoWindow.createBufferStrategy(1);
        }
    });
    return trayIcon;
}

From source file:net.mybox.mybox.ClientGUI.java

private void placeTrayIconAWT() {

    //Check the SystemTray support
    if (!SystemTray.isSupported()) {
        System.out.println("SystemTray is not supported");
        return;/*from w w w  . j  ava  2 s . com*/
    }
    final PopupMenu popup = new PopupMenu();

    final SystemTray tray = SystemTray.getSystemTray();

    // Create a popup menu components
    MenuItem aboutItem = new MenuItem("About Mybox");
    MenuItem opendirItem = new MenuItem("Open Directory");
    MenuItem prefsItem = new MenuItem("Preferences");
    MenuItem exitItem = new MenuItem("Quit Mybox");

    //Add components to popup menu

    popup.add(opendirItem);
    popup.add(pauseItem);
    popup.add(syncnowItem);

    popup.addSeparator();
    popup.add(prefsItem);
    popup.add(aboutItem);
    popup.add(connectionItem);
    popup.add(exitItem);

    trayIcon.setImageAutoSize(true);
    trayIcon.setToolTip("Mybox");

    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) {
            launchFileBrowser();
        }
    });

    aboutItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //JOptionPane.showMessageDialog(null, "Mybox!");

            Dialog dialog = new Dialog((ClientGUI) client.clientGui, "Mybox...");
            dialog.setVisible(true);

            //MessageDialog dialog = new InfoMessageDialog(null, "Mybox", "... is awesome!");//ErrorMessageDialog(null, "Error", message);
            //dialog.setTitle("About Mybox");
            //dialog.run();
            //dialog.hide();
        }
    });

    prefsItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //window.showAll();
            ((ClientGUI) client.clientGui).setVisible(true);
        }
    });

    syncnowItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            client.FullSync();
        }
    });

    opendirItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            launchFileBrowser();
        }
    });

    pauseItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (pauseItem.getLabel().equals("Pause Syncing")) {
                client.pause();
                pauseItem.setLabel("Unpause Syncing");
            } else if (pauseItem.getLabel().equals("Unpause Syncing")) {
                client.unpause();
                pauseItem.setLabel("Pause Syncing");
            }
        }
    });

    connectionItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (connectionItem.getLabel().equals("Connect")) {
                client.start();
            } else if (connectionItem.getLabel().equals("Disconnect")) {
                client.stop();
            }
        }
    });

    exitItem.addActionListener(new ActionListener() {

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

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  a2 s .co  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:com.darkenedsky.reddit.traders.RedditTraders.java

/**
 * Construct a new RedditTraders instance.
 * //from ww  w.  j a v  a2 s. c om
 * 
 */
public RedditTraders() {

    // Load XML configuration file, connect to DB and connect to Reddit API
    try {
        config = new Configuration();
        addListener(new Help(this));
        addListener(new About(this));
        addListener(new Confirm(this));
        addListener(new Activate(this));
        listeners.put("DEACTIVATE", new Activate(this));
        addListener(new CountAllSubs(this));
        addListener(new ModHelp(this));
        addListener(new AcceptModInvite(this));
        addListener(new Install(this));
        addListener(new Cake(this));
        addListener(new Trade(this));
        addListener(new TopTraders(this, "TOP20", 20));
        addListener(new Lookup(this));
        addListener(new SetLegacy(this));
        addListener(new SetTextFlair(this));
        addListener(new ViewFlair(this));
        addListener(new Resolve(this));
        listeners.put("BLAME", new Resolve(this));
        listeners.put("CLOSE", new Resolve(this));
        addListener(new SetFlair(this));
        listeners.put("REMOVEFLAIR", new SetFlair(this));
        addListener(new SetModFlair(this));
        listeners.put("REMOVEMODFLAIR", new SetModFlair(this));
        addListener(new SetBlameBan(this));
        addListener(new SetList(this));
        listeners.put("SETHAVELIST", new SetList(this));
        addListener(new GetList(this));
        listeners.put("HAVELIST", new GetList(this));
        addListener(new SetAccountAgeRequirement(this));
        addListener(new SetVerifiedEmail(this));
        addListener(new Undo(this));
        addListener(new LastTrades(this, "LAST10", 10));
        addListener(new SetCheckBan(this));
        addListener(new SetDaysBetween(this));

        // Build a system tray icon
        SystemTray tray = SystemTray.getSystemTray();

        PopupMenu popup = new PopupMenu();

        MenuItem todaysLog = new MenuItem("Today's Log");
        todaysLog.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new TextFrame("logs/RedditTraders.log").setVisible(true);
                } catch (Exception e1) {
                    LOG.error(e1);
                }
            }
        });
        popup.add(todaysLog);

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

        Image image = Toolkit.getDefaultToolkit().getImage("reddit.png");
        TrayIcon trayIcon = new TrayIcon(image, "RedditTraders " + config.getVersion(), popup);
        trayIcon.setImageAutoSize(true);
        tray.add(trayIcon);

        LOG.debug("RedditTraders launched OK.");
    } catch (Exception x) {
        x.printStackTrace();
        System.exit(0);
    }
}

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

private static void createSysTray(final String url, final Server srv) {
    final TrayIcon trayIcon;
    try {/*from   ww w . ja va  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:application.Main.java

public void createTrayIcon(final Stage stage) {
    // if the operating system
    // supports the system tray
    if (SystemTray.isSupported()) {
        // get the SystemTray instance
        SystemTray tray = SystemTray.getSystemTray();
        // load an image
        java.awt.Image image = null;
        try {/*from www.  ja  va2 s.c  o m*/
            //                File file = new File(iconLocation);
            //                image = ImageIO.read(file);
            URL urlIcon = Main.class.getResource(iconLocation);
            image = ImageIO.read(urlIcon);
        } catch (IOException ex) {
            System.out.println(ex);
        }

        stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent t) {
                hide(stage);
            }
        });

        // create an action listener to listen for default action executed on the tray icon
        final ActionListener closeListener = new ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent e) {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        stage.close();
                        controller.terminate();
                        //                           // fileWatcher.setTerminateWatching(Boolean.TRUE);
                        System.out.println(applicationTitle + " terminated!");
                        Platform.exit();
                        System.exit(0);
                    }
                });
            }
        };

        ActionListener showListener = new ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent e) {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        stage.show();
                    }
                });
            }
        };

        // create a pop-up menu
        PopupMenu popupMenu = new PopupMenu();

        MenuItem nameItem = new MenuItem(applicationTitle);
        nameItem.addActionListener(showListener);
        popupMenu.add(nameItem);

        popupMenu.addSeparator();

        MenuItem showItem = new MenuItem("Show");
        showItem.addActionListener(showListener);
        popupMenu.add(showItem);

        MenuItem closeItem = new MenuItem("Close");
        closeItem.addActionListener(closeListener);
        popupMenu.add(closeItem);

        /// ... add other menu items

        // construct a TrayIcon, scaling the image to 16x16 (the default dimensions of a tray icon)
        trayIcon = new TrayIcon(image.getScaledInstance(24, 24, Image.SCALE_DEFAULT), applicationTitle,
                popupMenu);
        // set the TrayIcon properties
        trayIcon.addActionListener(showListener);

        // add the tray image
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            System.err.println(e);
        }
    }
}

From source file:de.tbuchloh.kiskis.gui.MainFrame.java

/**
 * @see de.tbuchloh.kiskis.gui.systray.IMainFrame#getPopupMenu()
 *///from  w  ww. j a va2s. c o  m
@Override
public PopupMenu getPopupMenu() {
    final PopupMenu popup = new PopupMenu();
    for (final Action act : _main.getPopupActions()) {
        if (act == null) {
            popup.addSeparator();
        } else {
            final MenuItem mi = new MenuItem((String) act.getValue(Action.NAME));
            mi.setEnabled(act.isEnabled());
            mi.addActionListener(act);
            mi.setFont(new Font("Arial", Font.BOLD, 12));
            popup.add(mi);
        }
    }
    return popup;
}

From source file:org.nebulaframework.ui.swing.node.NodeMainUI.java

/**
 * System Tray Icon Pop Up Menu//from   w  w  w .  j  av  a 2s  .c o  m
 * 
 * @return PopupMenu
 */
private PopupMenu createTrayPopup() {
    PopupMenu trayPopup = new PopupMenu();

    // About
    MenuItem aboutItem = new MenuItem("About");
    aboutItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            showAbout();
        }

    });
    trayPopup.add(aboutItem);

    trayPopup.addSeparator();

    // Shutdown Node
    MenuItem shutdownItem = new MenuItem("Shutdown");
    shutdownItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            doShutdownNode();
        }

    });
    trayPopup.add(shutdownItem);

    return trayPopup;
}

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 {//from   w  w  w .java2 s  . 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();
    }
}