Example usage for javax.swing JMenuItem addActionListener

List of usage examples for javax.swing JMenuItem addActionListener

Introduction

In this page you can find the example usage for javax.swing JMenuItem addActionListener.

Prototype

public void addActionListener(ActionListener l) 

Source Link

Document

Adds an ActionListener to the button.

Usage

From source file:Main.java

public static JMenuItem createMenuItem(String text, Icon icon, String toolTip, ActionListener... listeners) {
    JMenuItem result = new JMenuItem(text, icon);

    if (toolTip != null) {
        result.setToolTipText(toolTip);/*from w  w w . j  a  v  a  2s  .c o m*/
    }

    for (ActionListener listener : listeners) {
        result.addActionListener(listener);
    }

    return result;
}

From source file:lu.lippmann.cdb.ext.hydviga.ui.GapsUIUtil.java

private static void addExportPopupMenu(final Instances ds, final ChartPanel cp) {
    cp.addChartMouseListener(new ChartMouseListener() {
        public void chartMouseClicked(ChartMouseEvent e) {
            final JPopupMenu jPopupMenu = new JPopupMenu("feur");
            final JMenuItem mi1 = new JMenuItem("Export as CSV");
            mi1.addActionListener(new ActionListener() {
                @Override/* w  w  w . j a  va2s .  c o  m*/
                public void actionPerformed(final ActionEvent e) {
                    final JFileChooser fc = new JFileChooser();
                    fc.setAcceptAllFileFilterUsed(false);
                    final int returnVal = fc.showSaveDialog(cp);
                    if (returnVal == JFileChooser.APPROVE_OPTION) {
                        try {
                            final File file = fc.getSelectedFile();
                            WekaDataAccessUtil.saveInstancesIntoCSVFile(ds, file);
                        } catch (final Exception ee) {
                            ee.printStackTrace();
                        }
                    }
                }
            });
            jPopupMenu.add(mi1);
            jPopupMenu.show(cp, e.getTrigger().getX(), e.getTrigger().getY());
        }

        public void chartMouseMoved(ChartMouseEvent e) {
        }
    });
}

From source file:LookAndFeelPrefs.java

/**
 * Create a menu of radio buttons listing the available Look and Feels. When
 * the user selects one, change the component hierarchy under frame to the new
 * LAF, and store the new selection as the current preference for the package
 * containing class c.//  w  w w .  j av  a  2  s .c o m
 */
public static JMenu createLookAndFeelMenu(final Class prefsClass, final ActionListener listener) {
    // Create the menu
    final JMenu plafmenu = new JMenu("Look and Feel");

    // Create an object used for radio button mutual exclusion
    ButtonGroup radiogroup = new ButtonGroup();

    // Look up the available look and feels
    UIManager.LookAndFeelInfo[] plafs = UIManager.getInstalledLookAndFeels();

    // Find out which one is currently used
    String currentLAFName = UIManager.getLookAndFeel().getClass().getName();

    // Loop through the plafs, and add a menu item for each one
    for (int i = 0; i < plafs.length; i++) {
        String plafName = plafs[i].getName();
        final String plafClassName = plafs[i].getClassName();

        // Create the menu item
        final JMenuItem item = plafmenu.add(new JRadioButtonMenuItem(plafName));
        item.setSelected(plafClassName.equals(currentLAFName));

        // Tell the menu item what to do when it is selected
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                // Set the new look and feel
                try {
                    UIManager.setLookAndFeel(plafClassName);
                } catch (UnsupportedLookAndFeelException e) {
                    // Sometimes a Look-and-Feel is installed but not
                    // supported, as in the Windows LaF on Linux platforms.
                    JOptionPane.showMessageDialog(plafmenu,
                            "The selected Look-and-Feel is " + "not supported on this platform.",
                            "Unsupported Look And Feel", JOptionPane.ERROR_MESSAGE);
                    item.setEnabled(false);
                } catch (Exception e) { // ClassNotFound or Instantiation
                    item.setEnabled(false); // shouldn't happen
                }

                // Make the selection persistent by storing it in prefs.
                Preferences p = Preferences.userNodeForPackage(prefsClass);
                p.put(PREF_NAME, plafClassName);

                // Invoke the supplied action listener so the calling
                // application can update its components to the new LAF
                // Reuse the event that was passed here.
                listener.actionPerformed(event);
            }
        });

        // Only allow one menu item to be selected at once
        radiogroup.add(item);
    }

    return plafmenu;
}

From source file:ShowComponent.java

/**
 * This static method queries the system to find out what Pluggable
 * Look-and-Feel (PLAF) implementations are available. Then it creates a
 * JMenu component that lists each of the implementations by name and allows
 * the user to select one of them using JRadioButtonMenuItem components.
 * When the user selects one, the selected menu item traverses the component
 * hierarchy and tells all components to use the new PLAF.
 *///from  w  ww  .j a v a2 s .  c o  m
public static JMenu createPlafMenu(final JFrame frame) {
    // Create the menu
    JMenu plafmenu = new JMenu("Look and Feel");

    // Create an object used for radio button mutual exclusion
    ButtonGroup radiogroup = new ButtonGroup();

    // Look up the available look and feels
    UIManager.LookAndFeelInfo[] plafs = UIManager.getInstalledLookAndFeels();

    // Loop through the plafs, and add a menu item for each one
    for (int i = 0; i < plafs.length; i++) {
        String plafName = plafs[i].getName();
        final String plafClassName = plafs[i].getClassName();

        // Create the menu item
        JMenuItem item = plafmenu.add(new JRadioButtonMenuItem(plafName));

        // Tell the menu item what to do when it is selected
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    // Set the new look and feel
                    UIManager.setLookAndFeel(plafClassName);
                    // Tell each component to change its look-and-feel
                    SwingUtilities.updateComponentTreeUI(frame);
                    // Tell the frame to resize itself to the its
                    // children's new desired sizes
                    frame.pack();
                } catch (Exception ex) {
                    System.err.println(ex);
                }
            }

        });

        // Only allow one menu item to be selected at once
        radiogroup.add(item);
    }
    return plafmenu;
}

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

/**
 * Creates a tray menu with the given <tt>name</tt>, text given by
 * <tt>textID</tt> and icon given by <tt>iconID</tt>. The <tt>listener</tt>
 * is handling item events and the <tt>swing</tt> value indicates if we
 * should create a Swing menu item or and an AWT item.
 * @param name the name of the item/*  www .  j  a va2  s  . c  o  m*/
 * @param textID the identifier of the text in the localization resources
 * @param iconID the identifier of the icon in the image resources
 * @param listener the <tt>ActionListener</tt> handling action events
 * @param swing indicates if we should create a Swing menu item or an AWT
 * item
 * @return a reference to the newly created item
 */
private static Object createTrayMenuItem(String name, String textID, String iconID, ActionListener listener,
        boolean swing) {
    String text = Resources.getString(textID);
    Object trayMenuItem;
    if (swing) {
        JMenuItem menuItem = new JMenuItem(text, Resources.getImage(iconID));
        menuItem.setName(name);
        menuItem.addActionListener(listener);
        trayMenuItem = menuItem;
    } else {
        MenuItem menuItem = new MenuItem(text);
        menuItem.setName(name);
        menuItem.addActionListener(listener);
        trayMenuItem = menuItem;
    }
    return trayMenuItem;
}

From source file:OAT.ui.util.UiUtil.java

public static JMenuItem newMenuItem(String name, KeyStroke keyStroke, final Runnable runnable) {
    JMenuItem menuItem = new JMenuItem(name);

    menuItem.setAccelerator(keyStroke);/*  w  ww  .j a  v  a 2 s.c  o  m*/

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

        @Override
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            java.awt.EventQueue.invokeLater(runnable);
        }
    });

    return menuItem;
}

From source file:net.sf.mzmine.chartbasics.graphicsexport.ChartExportUtil.java

/**
 * Add export dialog to popup menu of a chartpanel
 * //from  ww w  .  jav a  2  s.  c  om
 * @param plotChartPanel
 */
public static void addExportDialogToMenu(final ChartPanel cp) {
    JMenuItem exportGraphics = new JMenuItem("Export graphics...");
    exportGraphics.addActionListener(e -> GraphicsExportDialog.openDialog(cp.getChart()));
    // add to menu
    cp.getPopupMenu().add(exportGraphics);
}

From source file:com.net2plan.gui.GUINet2Plan.java

/**
 * Refresh main menu with currently loaded GUI plugins.
 *
 * @since 0.3.1/*from  w ww.ja v  a  2s .  c  o  m*/
 */
public static void refreshMenu() {
    instance.usedKeyStrokes.clear();

    while (instance.menu.getComponentCount() > 2)
        instance.menu.remove(1);

    for (Class<? extends Plugin> plugin : PluginSystem.getPlugins(IGUIModule.class)) {
        try {
            IGUIModule pluginInstance = ((Class<? extends IGUIModule>) plugin).newInstance();
            String menuName = pluginInstance.getMenu();
            JMenuItem item = getCurrentMenu(instance.menu, null, menuName);
            item.addActionListener(instance);

            KeyStroke keystroke = pluginInstance.getKeyStroke();
            if (keystroke != null && !instance.usedKeyStrokes.contains(keystroke)) {
                item.setAccelerator(keystroke);
                instance.usedKeyStrokes.add(keystroke);
            }

            instance.itemObject.put(item, plugin);
        } catch (NoClassDefFoundError e) {
            throw new Net2PlanException("Class " + e.getMessage() + " cannot be found. A dependence for "
                    + plugin.getSimpleName() + " is missing?");
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    instance.menu.revalidate();
}

From source file:SimpleMenu.java

/** The convenience method that creates menu panes */
public static JMenu create(ResourceBundle bundle, String menuname, String[] itemnames,
        ActionListener listener) {
    // Get the menu title from the bundle. Use name as default label.
    String menulabel;/*from  w w  w  .  j a  va2  s .  co  m*/
    try {
        menulabel = bundle.getString(menuname + ".label");
    } catch (MissingResourceException e) {
        menulabel = menuname;
    }

    // Create the menu pane.
    JMenu menu = new JMenu(menulabel);

    // For each named item in the menu.
    for (int i = 0; i < itemnames.length; i++) {
        // Look up the label for the item, using name as default.
        String itemlabel;
        try {
            itemlabel = bundle.getString(menuname + "." + itemnames[i] + ".label");
        } catch (MissingResourceException e) {
            itemlabel = itemnames[i];
        }

        JMenuItem item = new JMenuItem(itemlabel);

        // Look up an accelerator for the menu item
        try {
            String acceleratorText = bundle.getString(menuname + "." + itemnames[i] + ".accelerator");
            item.setAccelerator(KeyStroke.getKeyStroke(acceleratorText));
        } catch (MissingResourceException e) {
        }

        // Register an action listener and command for the item.
        if (listener != null) {
            item.addActionListener(listener);
            item.setActionCommand(itemnames[i]);
        }

        // Add the item to the menu.
        menu.add(item);
    }

    // Return the automatically created localized menu.
    return menu;
}

From source file:Main.java

/**
 * Creates a copy of this menu item, whose contents update automatically
 * whenever the original menu item changes.
 *//*from  ww  w.  ja  va2 s .com*/
public static JMenuItem cloneMenuItem(final JMenuItem item) {
    if (item == null)
        return null;
    JMenuItem jmi;
    if (item instanceof JMenu) {
        final JMenu menu = (JMenu) item;
        final JMenu jm = new JMenu();
        final int count = menu.getItemCount();
        for (int i = 0; i < count; i++) {
            final JMenuItem ijmi = cloneMenuItem(menu.getItem(i));
            if (ijmi == null)
                jm.addSeparator();
            else
                jm.add(ijmi);
        }
        jmi = jm;
    } else
        jmi = new JMenuItem();
    final ActionListener[] l = item.getActionListeners();
    for (int i = 0; i < l.length; i++)
        jmi.addActionListener(l[i]);
    jmi.setActionCommand(item.getActionCommand());
    syncMenuItem(item, jmi);
    linkMenuItem(item, jmi);
    return jmi;
}