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: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  ww w .j a va  2s.  c om*/
        }
    });
    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: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 w w .  j  a va2  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: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: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);

    final MenuItem exitItem = new MenuItem(localization.getString("tray.menu.quit"));
    exitItem.addActionListener(e -> exitCommand.run());
    popup.add(exitItem);/*from  w w w.  j a  va 2 s . c  o  m*/

    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: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 {/* w  w  w .j  a v  a2s.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:MonitorSaurausRex.MainMenu.java

private static void createAndShowGUI() {
    //Check the SystemTray support
    if (!SystemTray.isSupported()) {
        System.out.println("SystemTray is not supported");
        return;/*from   ww w .j  ava2 s. c  o  m*/
    }
    final PopupMenu popup = new PopupMenu();
    final TrayIcon trayIcon = new TrayIcon(createImage("Logo.png", "tray icon"));
    final SystemTray tray = SystemTray.getSystemTray();
    trayIcon.setImageAutoSize(true);
    // Create a popup menu components
    MenuItem aboutItem = new MenuItem("About");
    CheckboxMenuItem cb1 = new CheckboxMenuItem("Menu");
    CheckboxMenuItem cb2 = new CheckboxMenuItem("Quarantine!");
    Menu displayMenu = new Menu("Actions");
    MenuItem errorItem = new MenuItem("Cut Connections");
    MenuItem warningItem = new MenuItem("Send Reports");
    MenuItem infoItem = new MenuItem("Kill Processes");
    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);
    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,
                    "MonitorSaurausRex, Designed for sec203 group project. The program was designed to be piece of software to combat Ransomware"
                            + " in a corparate enviroment. The Project was created by Luke Barlow, Dayan Patel, Rob Shire and Sian Skiggs.");
        }
    });

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

    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);

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

From source file:org.apache.oodt.cas.workflow.gui.perspective.view.impl.DefaultTreeView.java

private PopupMenu createPopupMenu(final ViewState state) {
    final String ACTIONS_POP_MENU_NAME = "Actions";
    final String VIEW_CONDITION_MAP = "View...";
    PopupMenu actionsMenu = new PopupMenu(ACTIONS_POP_MENU_NAME);
    actionsMenu.add(new MenuItem(VIEW_CONDITION_MAP));
    actionsMenu.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            if (e.getActionCommand().equals(VIEW_CONDITION_MAP)) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getSelectionPath()
                        .getLastPathComponent();
                ModelGraph graphToFocus = null;
                if (Boolean.parseBoolean(state.getFirstPropertyValue(EXPAND_PRECONDITIONS))
                        || Boolean.parseBoolean(state.getFirstPropertyValue(EXPAND_POSTCONDITIONS))) {
                    // if (node.getUserObject() instanceof String &&
                    // (node.getUserObject().equals("pre-conditions") ||
                    // node.getUserObject().equals("post-conditions"))) {
                    ModelGraph graph = state.getSelected();
                    if (Boolean.parseBoolean(state.getFirstPropertyValue(EXPAND_PRECONDITIONS))) {
                        graphToFocus = graph.getPreConditions();
                    } else {
                        graphToFocus = graph.getPostConditions();
                    }/*from   w w  w  .  j av  a 2  s.c o  m*/
                } else if (node.getUserObject() instanceof ModelGraph) {
                    graphToFocus = (ModelGraph) node.getUserObject();
                }
                DefaultTreeView.this
                        .notifyListeners(new ViewChange.NEW_VIEW(graphToFocus, DefaultTreeView.this));
            }
        }

    });

    final String ORDER_SUB_POP_MENU_NAME = "Order";
    final String TO_FRONT_ITEM_NAME = "Move To Front";
    final String TO_BACK_ITEM_NAME = "Move To Back";
    final String FORWARD_ITEM_NAME = "Move Forward";
    final String BACKWARDS_ITEM_NAME = "Move Backwards";
    actionsMenu.add(orderSubMenu = new PopupMenu(ORDER_SUB_POP_MENU_NAME));
    orderSubMenu.add(new MenuItem(TO_FRONT_ITEM_NAME));
    orderSubMenu.add(new MenuItem(TO_BACK_ITEM_NAME));
    orderSubMenu.add(new MenuItem(FORWARD_ITEM_NAME));
    orderSubMenu.add(new MenuItem(BACKWARDS_ITEM_NAME));
    orderSubMenu.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            ModelGraph graph = state.getSelected();
            ModelGraph parent = graph.getParent();
            if (e.getActionCommand().equals(TO_FRONT_ITEM_NAME)) {
                if (parent.getChildren().remove(graph)) {
                    parent.getChildren().add(0, graph);
                }
            } else if (e.getActionCommand().equals(TO_BACK_ITEM_NAME)) {
                if (parent.getChildren().remove(graph)) {
                    parent.getChildren().add(graph);
                }
            } else if (e.getActionCommand().equals(FORWARD_ITEM_NAME)) {
                int index = parent.getChildren().indexOf(graph);
                if (index != -1) {
                    parent.getChildren().remove(index);
                    parent.getChildren().add(Math.max(0, index + 1), graph);
                }
            } else if (e.getActionCommand().equals(BACKWARDS_ITEM_NAME)) {
                int index = parent.getChildren().indexOf(graph);
                if (index != -1) {
                    parent.getChildren().remove(index);
                    parent.getChildren().add(Math.max(0, index - 1), graph);
                }
            }
            DefaultTreeView.this.notifyListeners();
            DefaultTreeView.this.refreshView(state);
        }

    });
    return actionsMenu;
}

From source file:de.mycrobase.jcloudapp.Main.java

public void run() {
    if (!SystemTray.isSupported()) {
        showErrorDialog("SystemTray is unsupported!");
        exit();/*from ww w.jav  a  2  s. c om*/
    }

    icon = new TrayIcon(ImageNormal);
    icon.setImageAutoSize(true);
    icon.setToolTip("JCloudApp");
    icon.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (!working) {
                working = true;
                doUploadClipboard();
                working = false;
            }
        }
    });

    MenuItem screen = new MenuItem("Take Screenshot");
    screen.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (!working) {
                working = true;
                doScreenshot();
                working = false;
            }
        }
    });

    MenuItem uploadClip = new MenuItem("Upload from Clipboard");
    uploadClip.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (!working) {
                working = true;
                doUploadClipboard();
                working = false;
            }
        }
    });

    MenuItem upload = new MenuItem("Upload File...");
    upload.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (!working) {
                working = true;
                doUploadFile();
                working = false;
            }
        }
    });

    MenuItem about = new MenuItem("About");
    about.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            doAbout();
        }
    });

    MenuItem quit = new MenuItem("Quit");
    quit.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            doQuit();
        }
    });

    PopupMenu popupMenu = new PopupMenu();
    popupMenu.add(screen);
    popupMenu.add(uploadClip);
    popupMenu.add(upload);
    popupMenu.add(about);
    popupMenu.addSeparator();
    popupMenu.add(quit);
    icon.setPopupMenu(popupMenu);

    try {
        SystemTray.getSystemTray().add(icon);
    } catch (AWTException ex) {
        showErrorDialog("No SystemTray found!\n" + ex);
        exit();
    }
}

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");
        }/*from  w  w  w  . j a va 2  s . c om*/
        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:de.tbuchloh.kiskis.gui.MainFrame.java

/**
 * @see de.tbuchloh.kiskis.gui.systray.IMainFrame#getPopupMenu()
 *///w  w w  .ja va  2  s . co 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;
}