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:edu.ku.brc.specify.utilapps.sp5utils.Sp5Forms.java

/**
 * @param args//from   w w  w .  jav a2s. c om
 */
public static void main(String[] args) {

    // Set App Name, MUST be done very first thing!
    UIRegistry.setAppName("Specify"); //$NON-NLS-1$

    log.debug("********* Current [" + (new File(".").getAbsolutePath()) + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

    UIRegistry.setEmbeddedDBPath(UIRegistry.getDefaultEmbeddedDBPath()); // on the local machine

    for (String s : args) {
        String[] pairs = s.split("="); //$NON-NLS-1$
        if (pairs.length == 2) {
            if (pairs[0].startsWith("-D")) //$NON-NLS-1$
            {
                //System.err.println("["+pairs[0].substring(2, pairs[0].length())+"]["+pairs[1]+"]");
                System.setProperty(pairs[0].substring(2, pairs[0].length()), pairs[1]);
            }
        } else {
            String symbol = pairs[0].substring(2, pairs[0].length());
            //System.err.println("["+symbol+"]");
            System.setProperty(symbol, symbol);
        }
    }

    // Now check the System Properties
    String appDir = System.getProperty("appdir");
    if (StringUtils.isNotEmpty(appDir)) {
        UIRegistry.setDefaultWorkingPath(appDir);
    }

    String appdatadir = System.getProperty("appdatadir");
    if (StringUtils.isNotEmpty(appdatadir)) {
        UIRegistry.setBaseAppDataDir(appdatadir);
    }

    Logger logger = LogManager.getLogger("edu.ku.brc");
    if (logger != null) {
        logger.setLevel(Level.ALL);
        System.out.println("Setting " + logger.getName() + " to " + logger.getLevel());
    }

    logger = LogManager.getLogger(edu.ku.brc.dbsupport.HibernateUtil.class);
    if (logger != null) {
        logger.setLevel(Level.INFO);
        System.out.println("Setting " + logger.getName() + " to " + logger.getLevel());
    }

    // Create Specify Application
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            try {
                if (!System.getProperty("os.name").equals("Mac OS X")) {
                    UIManager.setLookAndFeel(new Plastic3DLookAndFeel());
                    PlasticLookAndFeel.setPlasticTheme(new DesertBlue());
                }

                final Sp5Forms frame = new Sp5Forms();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setBounds(0, 0, 800, 800);

                new MacOSAppHandler(frame);

                frame.startup();

                frame.createUI();
                JMenuBar mb = new JMenuBar();
                JMenu fileMenu = new JMenu("File");

                JMenuItem openMI = new JMenuItem("Open XML File");
                fileMenu.add(openMI);
                openMI.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        frame.openXML();
                    }
                });

                JMenuItem openDBMI = new JMenuItem("Open Database");
                fileMenu.add(openDBMI);
                openDBMI.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        frame.openDB();
                    }
                });

                JMenuItem saveMI = new JMenuItem("Save");
                fileMenu.add(saveMI);
                saveMI.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        frame.saveXML();
                    }
                });

                if (!UIHelper.isMacOS()) {
                    fileMenu.addSeparator();
                    JMenuItem mi = new JMenuItem("Exit");
                    fileMenu.add(mi);

                    mi.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            System.exit(0);
                        }
                    });
                }

                mb.add(fileMenu);
                frame.setJMenuBar(mb);

                centerAndShow(frame);
            } catch (Exception e) {
                log.error("Can't change L&F: ", e);
            }
        }
    });
}

From source file:Main.java

public static JMenuItem item(String title, final ActionListener listener) {
    JMenuItem item = new JMenuItem(title);
    item.addActionListener(listener);
    return item;/*from   w w w . ja v  a2 s .  c o m*/
}

From source file:Main.java

/**
 * Add a menu item to a JPopupMenu attaching listener to it
 * //w ww  .  j a va2 s .  co m
 * @param menu
 * @param label
 * @param listener
 */
public static void addMenuItem(JPopupMenu menu, String label, ActionListener listener) {
    JMenuItem item = new JMenuItem(label);
    item.addActionListener(listener);
    menu.add(item);
}

From source file:Main.java

public static JMenuItem createMenuItem(String menuItemName, ActionListener actionListener) {
    JMenuItem menuItem = new JMenuItem(menuItemName);
    menuItem.addActionListener(actionListener);
    return menuItem;
}

From source file:Main.java

/**
 * Create menu item./* w  w w  . j av  a 2  s  . c o m*/
 * 
 * @param caption
 *            caption
 * @param action
 *            action
 * @return {@link JMenuItem}
 */
public static JMenuItem createMenuItem(String caption, Action action) {
    JMenuItem menuItem = new JMenuItem(caption);
    menuItem.addActionListener(action);
    return menuItem;
}

From source file:MenuDemo1.java

public static JMenuItem menuItem(String label, ActionListener listener, String command, int mnemonic,
        int acceleratorKey) {
    JMenuItem item = new JMenuItem(label);
    item.addActionListener(listener);
    item.setActionCommand(command);//w ww.j a v  a  2 s .  co m
    if (mnemonic != 0)
        item.setMnemonic((char) mnemonic);
    if (acceleratorKey != 0)
        item.setAccelerator(KeyStroke.getKeyStroke(acceleratorKey, java.awt.Event.CTRL_MASK));
    return item;
}

From source file:MenuDemo1.java

public static JMenuItem radioItem(String label, ActionListener listener, String command,
        ButtonGroup mutExGroup) {
    JMenuItem item = new JRadioButtonMenuItem(label);
    item.addActionListener(listener);
    item.setActionCommand(command);/* w w  w. j  a va 2  s.co  m*/
    mutExGroup.add(item);
    return item;
}

From source file:Main.java

public static JMenuItem buildMenuItem(String text, String actioncmd, ActionListener al) {
    JMenuItem mi = new JMenuItem();
    mi.setBackground(Color.lightGray);
    mi.setText(text);/*from   ww  w . jav a 2s .  c  o  m*/
    mi.setActionCommand(actioncmd);
    mi.addActionListener(al);
    return mi;
}

From source file:Main.java

public static JPopupMenu createStdEditPopupMenu(final JTextComponent[] fields) {
    final JPopupMenu popupMenu = new JPopupMenu();

    /* text fields popup menu: "Cut" */
    final JMenuItem cutMenuItem = new JMenuItem("Cut", 't');
    cutMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            final Component c = popupMenu.getInvoker();

            if (c instanceof JTextComponent) {
                ((JTextComponent) c).cut();
            }/*from   w ww. jav a 2  s. c  o  m*/
        }
    });
    popupMenu.add(cutMenuItem);

    /* text fields popup menu: "Copy" */
    final JMenuItem copyMenuItem = new JMenuItem("Copy", 'C');
    copyMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            final Component c = popupMenu.getInvoker();

            if (c instanceof JTextComponent) {
                ((JTextComponent) c).copy();
            }
        }
    });
    popupMenu.add(copyMenuItem);

    /* text fields popup menu: "Paste" */
    final JMenuItem pasteMenuItem = new JMenuItem("Paste", 'P');
    pasteMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            final Component c = popupMenu.getInvoker();

            if (c instanceof JTextComponent) {
                ((JTextComponent) c).paste();
            }
        }
    });
    popupMenu.add(pasteMenuItem);
    popupMenu.addSeparator();

    /* text fields popup menu: "Select All" */
    final JMenuItem selectAllMenuItem = new JMenuItem("Select All", 'A');
    selectAllMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            final Component c = popupMenu.getInvoker();

            if (c instanceof JTextComponent) {
                ((JTextComponent) c).selectAll();
            }
        }
    });
    popupMenu.add(selectAllMenuItem);

    /* add mouse listeners to the specified fields */
    for (final JTextComponent f : fields) {
        f.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                processMouseEvent(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                processMouseEvent(e);
            }

            private void processMouseEvent(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    popupMenu.show(e.getComponent(), e.getX(), e.getY());
                    popupMenu.setInvoker(f);
                }
            }
        });
    }
    return popupMenu;
}

From source file:Main.java

static public JMenuItem createJMenuItem(JMenu pMenu, String pText, char pMnemonic, KeyStroke pAccelerator,
        ActionListener pActionListener) throws IllegalArgumentException {
    if (pMenu == null)
        throw new IllegalArgumentException("Menu is missing.");
    if (pText == null)
        throw new IllegalArgumentException("Missing text for menu item.");

    final JMenuItem menuItem = new JMenuItem(pText, pMnemonic);

    if (pAccelerator != null)
        menuItem.setAccelerator(pAccelerator);
    if (pActionListener != null)
        menuItem.addActionListener(pActionListener);
    if (defaultFont != null)
        menuItem.setFont(defaultFont);// w ww  . j  a va 2  s  .co m

    pMenu.add(menuItem);

    return menuItem;
}