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

void addPopup(Component c, String name) {
    PopupMenu pm = new PopupMenu();
    MenuItem mi = new MenuItem(name + "-1");
    mi.addActionListener(this);
    pm.add(mi);//from w  w  w. j  a  va2 s .  c  om

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

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

From source file:PopupDemo.java

public PopupDemo() {
    MenuBar mb = new MenuBar();
    setMenuBar(mb);//from   ww  w. j a va2  s.  com
    Menu m = new Menu("file");
    mb.add(m);
    MenuItem item = new MenuItem("file-1");
    item.addActionListener(this);
    m.add(item);
    item = new MenuItem("file-2");
    m.add(item);

    setSize(100, 100);
    setLayout(new BorderLayout());

    Label l = new Label("label");
    addPopup(l, "label");
    add(l, "North");

    Panel p = new Panel();
    addPopup(p, "Panel");
    add(p, "Center");

    Button b = new Button("button");
    addPopup(b, "button");
    add(b, "South");
}

From source file:SplashDemo.java

public SplashDemo() {
    super("SplashScreen demo");
    setSize(300, 200);/*from  w w w .  jav  a2  s.  c  o  m*/
    setLayout(new BorderLayout());
    Menu m1 = new Menu("File");
    MenuItem mi1 = new MenuItem("Exit");
    m1.add(mi1);
    mi1.addActionListener(this);
    this.addWindowListener(closeWindow);

    MenuBar mb = new MenuBar();
    setMenuBar(mb);
    mb.add(m1);
    final SplashScreen splash = SplashScreen.getSplashScreen();
    if (splash == null) {
        System.out.println("SplashScreen.getSplashScreen() returned null");
        return;
    }
    Graphics2D g = splash.createGraphics();
    if (g == null) {
        System.out.println("g is null");
        return;
    }
    for (int i = 0; i < 100; i++) {
        renderSplashFrame(g, i);
        splash.update();
        try {
            Thread.sleep(90);
        } catch (InterruptedException e) {
        }
    }
    splash.close();
    setVisible(true);
    toFront();
}

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 {/*from  w  w  w.  j  av a  2s.  co  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: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 a  va  2  s . c o  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:com.github.cmisbox.ui.UI.java

public void addWatch(String path) {
    final MenuItem mi = new MenuItem(path.replaceAll("/", ""));
    mi.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            try {
                Desktop.getDesktop().open(new File(Config.getInstance().getWatchParent(), mi.getLabel()));
            } catch (IOException e) {
                UI.this.log.error(e);
            }/*from  w  w w.  j  av  a 2  s . c  o  m*/

        }
    });
    this.popup.add(mi);
}

From source file:AnotherMenuDemo.java

AnotherMenuDemo(String s) {
    super("MenuDemo: " + s);

    Container cp = this;
    cp.setLayout(new FlowLayout());

    mb = new MenuBar();
    setMenuBar(mb); // Frame implements MenuContainer

    MenuItem mi;
    // The File Menu...
    fm = new Menu("File");
    fm.add(mi = new MenuItem("Open", new MenuShortcut('O')));
    mi.addActionListener(this);
    fm.add(mi = new MenuItem("Close", new MenuShortcut('W')));
    mi.addActionListener(this);
    fm.addSeparator();/*from w  ww  . j  a  v a 2 s .c o  m*/
    fm.add(mi = new MenuItem("Print", new MenuShortcut('P')));
    mi.addActionListener(this);
    fm.addSeparator();
    fm.add(mi = new MenuItem("Exit", new MenuShortcut('Q')));
    exitItem = mi; // save for action handler
    mi.addActionListener(this);
    mb.add(fm);

    // The Options Menu...
    om = new Menu("Options");
    cb = new CheckboxMenuItem("AutoSave");
    cb.setState(true);
    cb.addItemListener(this);
    om.add(cb);
    opSubm = new Menu("SubOptions");
    opSubm.add(new MenuItem("Alpha"));
    opSubm.add(new MenuItem("Gamma"));
    opSubm.add(new MenuItem("Delta"));
    om.add(opSubm);
    mb.add(om);

    // The Help Menu...
    hm = new Menu("Help");
    hm.add(mi = new MenuItem("About"));
    mi.addActionListener(this);
    hm.add(mi = new MenuItem("Topics"));
    mi.addActionListener(this);
    mb.add(hm);
    mb.setHelpMenu(hm); // needed for portability (Motif, etc.).

    // the main window
    cp.add(new Label("Menu Demo Window", 200, 150));
    pack();
}

From source file:coolmapplugin.impl.CMCyCommunicationPlugin.java

private void initMenuItems() {

    CoolMapMaster.getCMainFrame().addMenuSeparator("Plugin");
    MenuItem item = new MenuItem("Without aggregated values");
    CoolMapMaster.getCMainFrame().addMenuItem("Plugin/Cytoscape/Map nodes/", item, false, false);
    item.addActionListener(new MapSelectedToCytoscape());

    CoolMapMaster.getCMainFrame().addMenuSeparator("Plugin");
    item = new MenuItem("Find Cytoscape selection in CoolMap");
    CoolMapMaster.getCMainFrame().addMenuItem("Plugin/Cytoscape", item, false, false);
    item.addActionListener(new MapSelectedFromCytoscape());

    CoolMapMaster.getCMainFrame().addMenuSeparator("Plugin");
    item = new MenuItem("Mean");
    CoolMapMaster.getCMainFrame().addMenuItem("Plugin/Cytoscape/Map nodes/With aggregated values", item, false,
            false);//w  w  w.  j a  v  a  2s .co  m
    item.addActionListener(new MapSelectedToCytoscapeWithAggregatedValue(
            MapSelectedToCytoscapeWithAggregatedValue.AggregationType.MEAN));

    CoolMapMaster.getCMainFrame().addMenuSeparator("Plugin");
    item = new MenuItem("Sum");
    CoolMapMaster.getCMainFrame().addMenuItem("Plugin/Cytoscape/Map nodes/With aggregated values", item, false,
            false);
    item.addActionListener(new MapSelectedToCytoscapeWithAggregatedValue(
            MapSelectedToCytoscapeWithAggregatedValue.AggregationType.SUM));

    CoolMapMaster.getCMainFrame().addMenuSeparator("Plugin");
    item = new MenuItem("Max");
    CoolMapMaster.getCMainFrame().addMenuItem("Plugin/Cytoscape/Map nodes/With aggregated values", item, false,
            false);
    item.addActionListener(new MapSelectedToCytoscapeWithAggregatedValue(
            MapSelectedToCytoscapeWithAggregatedValue.AggregationType.MAX));

    CoolMapMaster.getCMainFrame().addMenuSeparator("Plugin");
    item = new MenuItem("Min");
    CoolMapMaster.getCMainFrame().addMenuItem("Plugin/Cytoscape/Map nodes/With aggregated values", item, false,
            false);
    item.addActionListener(new MapSelectedToCytoscapeWithAggregatedValue(
            MapSelectedToCytoscapeWithAggregatedValue.AggregationType.MIN));

    CoolMapMaster.getCMainFrame().addMenuSeparator("Plugin");
    item = new MenuItem("Median");
    CoolMapMaster.getCMainFrame().addMenuItem("Plugin/Cytoscape/Map nodes/With aggregated values", item, false,
            false);
    item.addActionListener(new MapSelectedToCytoscapeWithAggregatedValue(
            MapSelectedToCytoscapeWithAggregatedValue.AggregationType.MEDIAN));

}

From source file:com.loy.MainFrame.java

private void systemTray() {
    if (SystemTray.isSupported()) {
        ImageIcon icon = new ImageIcon(image);
        PopupMenu popupMenu = new PopupMenu();

        MenuItem itemShow = new MenuItem("Show");
        MenuItem itemExit = new MenuItem("Exit");
        itemExit.addActionListener(new ActionListener() {
            @Override//  w  w  w  .j  a v  a  2 s. c o m
            public void actionPerformed(ActionEvent e) {
                killProcess();
                System.exit(0);
            }
        });
        itemShow.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(true);
            }
        });

        popupMenu.add(itemShow);
        popupMenu.add(itemExit);
        TrayIcon trayIcon = new TrayIcon(icon.getImage(), "E-MICRO-SERVICE-START", popupMenu);
        SystemTray sysTray = SystemTray.getSystemTray();
        try {
            sysTray.add(trayIcon);
        } catch (AWTException e1) {
        }
    }
}

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  ww.jav  a2  s  .  c o m

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