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:SysTray.java

public void installSystemTray() throws Exception {
    PopupMenu menu = new PopupMenu();
    MenuItem exit = new MenuItem("Exit");
    exit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);//from   w w  w .  j  a  v  a  2  s.  c  o m
        }
    });
    menu.add(exit);

    TrayIcon icon = new TrayIcon(getImage(), "Java application as a tray icon", menu);
    icon.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "Hi!");
        }
    });
    SystemTray.getSystemTray().add(icon);
    Thread.sleep(3000);
    icon.displayMessage("Attention", "Please click here", TrayIcon.MessageType.WARNING);
}

From source file:AppletMenuBarDemo.java

public void init() {
    AppletMenuBar menubar = new AppletMenuBar();
    menubar.setForeground(Color.black);
    menubar.setHighlightColor(Color.red);
    menubar.setFont(new Font("helvetica", Font.BOLD, 12));
    this.setLayout(new BorderLayout());
    this.add(menubar, BorderLayout.NORTH);

    PopupMenu file = new PopupMenu();
    file.add("New...");
    file.add("Open...");
    file.add("Save As...");
    PopupMenu edit = new PopupMenu();
    edit.add("Cut");
    edit.add("Copy");
    edit.add("Paste");

    menubar.addMenu("File", file);
    menubar.addMenu("Edit", edit);
}

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  ww .j  a  v  a 2  s.  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:org.kootox.episodesmanager.ui.systray.EpisodesTrayIcon.java

public void create() {

    //Check the SystemTray support
    if (!SystemTray.isSupported()) {
        if (log.isInfoEnabled()) {
            log.info("SystemTray is not supported");
        }//w w  w  .  j  av a  2  s .co m
        return;
    }

    if (loaded) {
        return;
    }

    final PopupMenu popup = new PopupMenu();
    final TrayIcon trayIcon = new TrayIcon(createImage("systray.png", "tray icon"));
    final SystemTray tray = SystemTray.getSystemTray();

    // Create a popup menu components
    MenuItem display = new MenuItem("Display");
    MenuItem exit = new MenuItem("Exit");

    //Add components to popup menu
    popup.add(display);
    popup.addSeparator();
    popup.add(exit);

    trayIcon.setPopupMenu(popup);

    try {
        tray.add(trayIcon);
    } catch (AWTException e) {
        if (log.isDebugEnabled()) {
            log.debug("TrayIcon could not be added.");
        }
        return;
    }

    trayIcon.addMouseListener(new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent mouseEvent) {
            if (mouseEvent.getButton() == MouseEvent.BUTTON1) {
                showHide();
            }
        }

        @Override
        public void mousePressed(MouseEvent mouseEvent) {
            //Do nothing
        }

        @Override
        public void mouseReleased(MouseEvent mouseEvent) {
            //Do nothing
        }

        @Override
        public void mouseEntered(MouseEvent mouseEvent) {
            //Do nothing
        }

        @Override
        public void mouseExited(MouseEvent mouseEvent) {
            //Do nothing
        }
    });

    display.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            showHide();
        }
    });

    exit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            EpisodesManagerMainUI mainUI = EpisodesManagerContext.MAIN_UI_ENTRY_DEF.getContextValue(context);
            mainUI.close();
        }
    });

    loaded = true;
    if (log.isDebugEnabled()) {
        log.debug("Systray loaded");
    }
}

From source file:com.github.cmisbox.ui.UI.java

private UI() {
    this.log = LogFactory.getLog(this.getClass());
    try {// ww  w  .j ava  2  s  .  com
        this.available = !GraphicsEnvironment.isHeadless();

        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

        if (SystemTray.isSupported()) {

            this.tray = SystemTray.getSystemTray();
            Image image = ImageIO.read(this.getClass().getResource("images/cmisbox.png"));

            ActionListener exitListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    Main.exit(0);
                }
            };

            this.popup = new PopupMenu();
            MenuItem defaultItem = new MenuItem(Messages.exit);
            defaultItem.addActionListener(exitListener);
            this.popup.add(defaultItem);

            MenuItem loginItem = new MenuItem(Messages.login);
            loginItem.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent arg0) {
                    new LoginDialog();
                }
            });
            this.popup.add(loginItem);

            MenuItem treeItem = new MenuItem(Messages.showTree);
            treeItem.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent arg0) {
                    new TreeSelect();
                }
            });

            this.popup.add(treeItem);

            final TrayIcon trayIcon = new TrayIcon(image, UI.NOTIFY_TITLE, this.popup);

            trayIcon.setImageAutoSize(true);

            this.tray.add(trayIcon);

            this.notify(Messages.startupComplete);

        }

    } catch (Exception e) {
        this.log.error(e);
    }
}

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);/*  w w w . ja v a 2 s. com*/

    mi = new MenuItem(name + "-2");
    pm.add(mi);

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

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/* w w  w. j  av a 2 s . co  m*/
        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:org.cryptomator.ui.ExitUtil.java

private TrayIcon createTrayIcon(Runnable exitCommand) {
    final PopupMenu popup = new PopupMenu();

    final MenuItem showItem = new MenuItem(localization.getString("tray.menu.open"));
    showItem.addActionListener(this::restoreFromTray);
    popup.add(showItem);//from   w  w w  .  j a va  2 s.  c om

    final MenuItem exitItem = new MenuItem(localization.getString("tray.menu.quit"));
    exitItem.addActionListener(e -> exitCommand.run());
    popup.add(exitItem);

    final Image image;
    if (SystemUtils.IS_OS_MAC_OSX && isMacMenuBarDarkMode()) {
        image = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/tray_icon_mac_white.png"));
    } else if (SystemUtils.IS_OS_MAC_OSX) {
        image = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/tray_icon_mac_black.png"));
    } else {
        image = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/tray_icon.png"));
    }

    return new TrayIcon(image, localization.getString("app.name"), popup);
}

From source file:net.java.sip.communicator.impl.osdependent.jdic.TrayMenuFactory.java

/**
 * Creates a tray menu for the given system tray.
 *
 * @param tray the system tray for which we're creating a menu
 * @param swing indicates if we should create a Swing or an AWT menu
 * @return a tray menu for the given system tray (first) and the default
 *         menu item (second)/*w w w  .  j av a  2s  . co m*/
 */
public static Pair<Object, Object> createTrayMenu(SystrayServiceJdicImpl tray, boolean swing,
        boolean accountMenuSupported) {
    final Object trayMenu = swing ? new JPopupMenu() : new PopupMenu();
    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            TrayMenuFactory.actionPerformed(event);
        }
    };

    Boolean showOptions = OsDependentActivator.getConfigurationService()
            .getBoolean("net.java.sip.communicator.impl.gui.main.configforms." + "SHOW_OPTIONS_WINDOW", true);

    if (showOptions.booleanValue()) {
        add(trayMenu,
                createTrayMenuItem("settings",
                        (OSUtils.IS_MAC) ? "service.gui.PREFERENCES" : "service.gui.SETTINGS",
                        "service.systray.CONFIGURE_ICON", listener, swing));
    }

    add(trayMenu, createTrayMenuItem("addContact", "service.gui.ADD_CONTACT",
            "service.gui.icons.ADD_CONTACT_16x16_ICON", listener, swing));
    addSeparator(trayMenu);

    Boolean chatPresenceDisabled = OsDependentActivator.getConfigurationService()
            .getBoolean("net.java.sip.communicator.impl.gui.main.presence." + "CHAT_PRESENCE_DISABLED", false);

    if (!chatPresenceDisabled.booleanValue() && accountMenuSupported) {
        add(trayMenu, new StatusSubMenu(swing, accountMenuSupported).getMenu());
        addSeparator(trayMenu);
    }

    String showHideName;
    String showHideTextId;
    String showHideIconId;

    if (OsDependentActivator.getUIService().isVisible()) {
        showHideName = "service.gui.HIDE";
        showHideTextId = "service.gui.HIDE";
        showHideIconId = "service.gui.icons.SEARCH_ICON_16x16";
    } else {
        showHideName = "service.gui.SHOW";
        showHideTextId = "service.gui.SHOW";
        showHideIconId = "service.gui.icons.SEARCH_ICON_16x16";
    }

    final Object showHideMenuItem = createTrayMenuItem(showHideName, showHideTextId, showHideIconId, listener,
            swing);

    add(trayMenu, showHideMenuItem);

    add(trayMenu, createTrayMenuItem("service.gui.QUIT", "service.gui.QUIT", "service.systray.QUIT_MENU_ICON",
            listener, swing));

    OsDependentActivator.getUIService().addWindowListener(new WindowAdapter() {
        /**
         * Invoked when a window is activated.
         */
        @Override
        public void windowActivated(WindowEvent e) {
            changeTrayMenuItem(showHideMenuItem, "service.gui.HIDE", "service.gui.HIDE",
                    "service.gui.icons.SEARCH_ICON_16x16");
        }

        /**
         * Invoked when a window is de-activated.
         */
        @Override
        public void windowDeactivated(WindowEvent e) {
            changeTrayMenuItem(showHideMenuItem, "service.gui.SHOW", "service.gui.SHOW",
                    "service.gui.icons.SEARCH_ICON_16x16");
        }
    });

    return Pair.of(trayMenu, showHideMenuItem);
}

From source file:de.jakop.ngcalsync.application.TrayStarter.java

private void moveToTray(final Settings settings, final Application application) {

    final PopupMenu popup = new PopupMenu();

    // Create a pop-up menu components
    final MenuItem syncItem = new MenuItem(UserMessage.get().MENU_ITEM_SYNCHRONIZE());
    final CheckboxMenuItem schedulerItem = new CheckboxMenuItem(UserMessage.get().MENU_ITEM_SCHEDULER_ACTIVE());
    final MenuItem logItem = new MenuItem(UserMessage.get().MENU_ITEM_SHOW_LOG());
    final MenuItem aboutItem = new MenuItem(UserMessage.get().MENU_ITEM_ABOUT());
    final MenuItem exitItem = new MenuItem(UserMessage.get().MENU_ITEM_EXIT());

    // let the tray icon listen to sync events for state change
    application.addObserver(getTrayIcon());

    //Add components to pop-up menu
    popup.add(syncItem);//w  w w. j a v  a 2  s. c om
    popup.add(schedulerItem);
    popup.add(logItem);
    popup.add(aboutItem);
    popup.addSeparator();
    popup.add(exitItem);
    getTrayIcon().setPopupMenu(popup);

    application.reloadSettings();

    final JFrame logWindow = createLogWindow(Level.toLevel(settings.getPopupThresholdLevel(), Level.INFO));
    final JFrame aboutWindow = createAboutWindow();

    final ActionListener syncActionListener = createSyncActionListener(application.getScheduler());
    syncItem.addActionListener(syncActionListener);
    // sync also on double click
    getTrayIcon().addActionListener(syncActionListener);
    getTrayIcon().addMouseListener(createLogMouseListener(logWindow));

    schedulerItem.addItemListener(createSchedulerItemListener(settings, application));
    logItem.addActionListener(createLogActionListener(logWindow));
    aboutItem.addActionListener(createAboutActionListener(aboutWindow));
    exitItem.addActionListener(createExitActionListener(logWindow, aboutWindow));

    schedulerItem.setState(settings.isSchedulerStarted());
    toggleScheduler(settings.isSchedulerStarted(), settings, application);

}