Example usage for java.awt MenuItem addActionListener

List of usage examples for java.awt MenuItem addActionListener

Introduction

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

Prototype

public synchronized void addActionListener(ActionListener l) 

Source Link

Document

Adds the specified action listener to receive action events from this menu item.

Usage

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

private UI() {
    this.log = LogFactory.getLog(this.getClass());
    try {// w  w  w.  j  a v a 2  s . co  m
        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: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   ww 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: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/* w  w w .j  av  a  2  s .  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:MDIApp.java

private MenuBar createMenuBar() {
    ActionListener al = new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            String command = ae.getActionCommand();
            if (command.equals("Open")) {
                if (fd == null) {
                    fd = new FileDialog(MDIApp.this, "Open File", FileDialog.LOAD);
                    fd.setDirectory("/movies");
                }//from  ww  w  .j  a  v a  2  s.c o m
                fd.show();
                if (fd.getFile() != null) {
                    String filename = fd.getDirectory() + fd.getFile();
                    openFile("file:" + filename);
                }
            } else if (command.equals("Exit")) {
                dispose();
                System.exit(0);
            }
        }
    };

    MenuItem item;
    MenuBar mb = new MenuBar();
    // File Menu
    Menu mnFile = new Menu("File");
    mnFile.add(item = new MenuItem("Open"));
    item.addActionListener(al);
    mnFile.add(item = new MenuItem("Exit"));
    item.addActionListener(al);

    // Options Menu 
    Menu mnOptions = new Menu("Options");
    cbAutoLoop = new CheckboxMenuItem("Auto replay");
    cbAutoLoop.setState(true);
    mnOptions.add(cbAutoLoop);

    mb.add(mnFile);
    mb.add(mnOptions);
    return mb;
}

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

/**
 * @see de.tbuchloh.kiskis.gui.systray.IMainFrame#getPopupMenu()
 *//*w ww .j ava2 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;
}

From source file:com.sec.ose.osi.ui.frm.tray.JTrayIconApp.java

private PopupMenu createPopupMenu(int state) {
    PopupMenu popupMenu = new PopupMenu("PopupMenu");

    MenuItem miLogOut = new MenuItem("LogOut");
    MenuItem miOpen = new MenuItem("Open");
    MenuItem miExit = new MenuItem("Exit");
    MenuItem miAbout = new MenuItem("About");

    miLogOut.addActionListener(new java.awt.event.ActionListener() {

        public void actionPerformed(java.awt.event.ActionEvent e) {

            log.debug("actionPerformed() - LogOut");
            mEventHandler.handle(EventHandler.LOGOUT_MENU);
        }/*from  w  w w  .j a  v a2  s. c  o m*/

    });

    miOpen.addActionListener(new java.awt.event.ActionListener() {

        public void actionPerformed(java.awt.event.ActionEvent e) {

            log.debug("actionPerformed() - Open");
            mEventHandler.handle(EventHandler.OPEN_MENU);
        }

    });

    miAbout.addActionListener(new java.awt.event.ActionListener() {

        public void actionPerformed(java.awt.event.ActionEvent e) {

            log.debug("actionPerformed() - Help - SubSix");
            mEventHandler.handle(EventHandler.HELP_ABOUT);
        }

    });

    miExit.addActionListener(new java.awt.event.ActionListener() {

        public void actionPerformed(java.awt.event.ActionEvent e) {

            log.debug("actionPerformed() - Exit");
            mEventHandler.handle(EventHandler.EXIT_MENU);
        }
    });

    switch (state) {

    case BEFORE_LOGIN_STATE:
        popupMenu.add(miOpen);
        popupMenu.addSeparator();
        popupMenu.add(miAbout);
        popupMenu.addSeparator();
        popupMenu.add(miExit);
        break;

    case AFTER_LOGIN_STATE:
        popupMenu.add(miLogOut);
        popupMenu.addSeparator();
        popupMenu.add(miOpen);
        popupMenu.addSeparator();
        popupMenu.add(miAbout);
        popupMenu.addSeparator();
        popupMenu.add(miExit);
        break;
    }

    return popupMenu;
}

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 a  va 2 s .  c o m

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

/***************************************************************************
 * Construct a PlayerOfMedia. The Frame will have the title supplied by the
 * user. All initial actions on the PlayerOfMedia object are initiated
 * through its menu (or shotcut key).//  ww  w.  j a  v a 2  s  . c o m
 **************************************************************************/
PlayerOfMedia(String name) {

    super(name);
    ///////////////////////////////////////////////////////////
    // Setup the menu system: a "File" menu with Open and Quit.
    ///////////////////////////////////////////////////////////
    bar = new MenuBar();
    fileMenu = new Menu("File");
    MenuItem openMI = new MenuItem("Open...", new MenuShortcut(KeyEvent.VK_O));
    openMI.setActionCommand("OPEN");
    openMI.addActionListener(this);
    fileMenu.add(openMI);
    MenuItem quitMI = new MenuItem("Quit", new MenuShortcut(KeyEvent.VK_Q));
    quitMI.addActionListener(this);
    quitMI.setActionCommand("QUIT");
    fileMenu.add(quitMI);
    bar.add(fileMenu);
    setMenuBar(bar);

    ///////////////////////////////////////////////////////
    // Layout the frame, its position on screen, and ensure
    // window closes are dealt with properly, including
    // relinquishing the resources of any Player.
    ///////////////////////////////////////////////////////
    setLayout(new BorderLayout());
    setLocation(100, 100);
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            if (player != null) {
                player.stop();
                player.close();
            }
            System.exit(0);
        }
    });

    /////////////////////////////////////////////////////
    // Build the Dialog box by which the user can select
    // the media to play.
    /////////////////////////////////////////////////////
    selectionDialog = new Dialog(this, "Media Selection");
    Panel pan = new Panel();
    pan.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    mediaName = new TextField(40);
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 2;
    pan.add(mediaName, gbc);
    choose = new Button("Choose File...");
    gbc.ipadx = 10;
    gbc.ipady = 10;
    gbc.gridx = 2;
    gbc.gridwidth = 1;
    pan.add(choose, gbc);
    choose.addActionListener(this);
    open = new Button("Open");
    gbc.gridy = 1;
    gbc.gridx = 1;
    pan.add(open, gbc);
    open.addActionListener(this);
    cancel = new Button("Cancel");
    gbc.gridx = 2;
    pan.add(cancel, gbc);
    cancel.addActionListener(this);
    selectionDialog.add(pan);
    selectionDialog.pack();
    selectionDialog.setLocation(200, 200);

    ////////////////////////////////////////////////////
    // Build the error Dialog box by which the user can
    // be informed of any errors or problems.
    ////////////////////////////////////////////////////
    errorDialog = new Dialog(this, "Error", true);
    errorLabel = new Label("");
    errorDialog.add(errorLabel, "North");
    ok = new Button("OK");
    ok.addActionListener(this);
    errorDialog.add(ok, "South");
    errorDialog.pack();
    errorDialog.setLocation(150, 300);

    Manager.setHint(Manager.PLUGIN_PLAYER, new Boolean(true));
}

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  a 2 s .  co 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.rapidcontext.app.ui.ControlPanel.java

/**
 * Initializes the frame menu./*  ww  w .  jav a  2s  . c  o  m*/
 */
private void initializeMenu() {
    Menu menu;
    MenuItem item;

    // Create file menu
    if (!SystemUtils.IS_OS_MAC_OSX) {
        menu = new Menu("File");
        item = new MenuItem("Exit", new MenuShortcut(KeyEvent.VK_Q));
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                quit();
            }
        });
        menu.add(item);
        menuBar.add(menu);
        menu = new Menu("Help");
        item = new MenuItem("About");
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                about();
            }
        });
        menu.add(item);
        menuBar.add(menu);
    }

    // Fix Mac OS specific menus
    if (SystemUtils.IS_OS_MAC_OSX) {
        try {
            MacApplication.get().setAboutHandler(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    about();
                }
            });
            MacApplication.get().setPreferencesHandler(null);
        } catch (Exception ignore) {
            // Errors are ignored
        }
    }
}