Example usage for javax.swing JMenu add

List of usage examples for javax.swing JMenu add

Introduction

In this page you can find the example usage for javax.swing JMenu add.

Prototype

public JMenuItem add(Action a) 

Source Link

Document

Creates a new menu item attached to the specified Action object and appends it to the end of this menu.

Usage

From source file:Main.java

/**
 * Creates a {@link JMenu} from the given list and action map.
 * //ww w  .  jav a2  s .  c  om
 * @param actionMap
 * @param list
 * @return
 */
public static JMenu createMenu(ActionMap actionMap, List<?> list) {
    // The first item will be the action for the JMenu
    final Action titleAction = actionMap.get(list.get(0));
    if (titleAction == null) {
        return null;
    }
    final JMenu menu = new JMenu();
    menu.setAction(titleAction);

    // The rest of the items represent the menu items.
    for (Object element : list.subList(1, list.size())) {
        if (element == null) {
            menu.addSeparator();
        } else if (element instanceof List<?>) {
            JMenu newMenu = createMenu(actionMap, (List<?>) element);
            if (newMenu != null) {
                menu.add(newMenu);
            }
        } else if (element.getClass().isArray()) {
            JMenu newMenu = createMenu(actionMap, (Object[]) element);
            if (newMenu != null) {
                menu.add(newMenu);
            }
        } else {
            final Action action = actionMap.get(element);
            if (action == null) {
                continue;
            } else {
                menu.add(createMenuItem(action));
            }
        }
    }
    return menu;
}

From source file:net.sf.jabref.gui.menus.RightClickMenu.java

/**
 * Remove all types from the menu./*from  w ww  . jav  a 2  s .c  o  m*/
 * Then cycle through all available values, and add them.
 */
public static void populateSpecialFieldMenu(JMenu menu, SpecialField field, JabRefFrame frame) {
    menu.setText(field.getMenuString());
    menu.setIcon(((IconTheme.FontBasedIcon) field.getRepresentingIcon()).createSmallIcon());
    for (SpecialFieldValue val : field.getValues()) {
        menu.add(val.getMenuAction(frame));
    }
}

From source file:com.googlecode.blaisemath.app.MenuConfig.java

/**
 * Build a menu from a given map entry with key the menu name, and value a list
 * or map that determines the menu items.
 * @param name menu name//  w w w . j  a v a2  s .  c o  m
 * @param menuContent content of menu
 * @param am string/action mapping
 * @return menu
 */
static JMenu createMenu(String name, List menuContent, Map<String, Action> am) {
    JMenu menu = new JMenu(name);
    for (JMenuItem el : createMenuItemList(menuContent, am)) {
        if (el == null) {
            menu.addSeparator();
        } else {
            menu.add(el);
        }
    }
    return menu;
}

From source file:net.sf.jabref.gui.RightClickMenu.java

/**
 * Remove all types from the menu.//from w w  w .  j ava2s  .  c  om
 * Then cycle through all available values, and add them.
 */
public static void populateSpecialFieldMenu(JMenu menu, SpecialField field, JabRefFrame frame) {
    //menu.removeAll();
    menu.setText(field.getMenuString());
    menu.setIcon(((IconTheme.FontBasedIcon) field.getRepresentingIcon()).createSmallIcon());
    for (SpecialFieldValue val : field.getValues()) {
        menu.add(val.getMenuAction(frame));
    }
}

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);/*from w  w w .j  a v a2s.  c o m*/

    pMenu.add(menuItem);

    return menuItem;
}

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

private static JMenuItem getCurrentMenu(JMenuBar menubar, JMenu parent, String itemName) {
    int pos = itemName.indexOf('|');
    if (pos == -1) {
        if (parent == null)
            throw new RuntimeException("Bad");

        JMenuItem menuItem = new JMenuItem(itemName);
        parent.add(menuItem);

        return menuItem;
    } else {/*ww  w .j a  v a 2s . c  o  m*/
        String parentName = itemName.substring(0, pos);
        JMenu new_parent = null;

        MenuElement[] children;
        if (menubar != null)
            children = menubar.getSubElements();
        else if (parent != null)
            children = parent.getSubElements();
        else
            throw new RuntimeException("Bad");

        for (MenuElement item : children) {
            if (!(item instanceof JMenu) || !((JMenu) item).getText().equalsIgnoreCase(parentName))
                continue;

            new_parent = (JMenu) item;
            break;
        }

        if (new_parent == null) {
            new_parent = new JMenu(parentName);

            if (menubar != null) {
                if (parentName.equals("Tools")) {
                    menubar.add(new_parent, 1);
                    new_parent.setMnemonic('T');
                } else {
                    menubar.add(new_parent, menubar.getComponentCount() - 1);
                }
            } else if (parent != null) {
                parent.add(new_parent);
            } else {
                throw new RuntimeException("Bad");
            }
        }

        String new_itemName = itemName.substring(pos + 1);

        return getCurrentMenu(null, new_parent, new_itemName);
    }
}

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 w w.jav a  2 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: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./*from www  .  j av a 2  s  . com*/
 */
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:Main.java

/**
 * Populates a {@link JMenuBar} from the given list and {@link ActionMap}.
 * /* w w w. java 2 s .  c om*/
 * @param menubar
 * @param actionMap
 * @param list
 * @return
 */
public static void populate(JMenuBar menubar, ActionMap actionMap, List<?> list) {
    JMenu menu = null;

    for (Object element : list) {
        if (element == null) {
            if (menu != null) {
                menu.addSeparator();
            }
        } else if (element instanceof List<?>) {
            menu = createMenu(actionMap, (List<?>) element);
            if (menu != null) {
                menubar.add(menu);
            }
        } else if (element.getClass().isArray()) {
            menu = createMenu(actionMap, (Object[]) element);
            if (menu != null) {
                menubar.add(menu);
            }
        } else {
            if (menu != null) {
                menu.add(actionMap.get(element));
            }
        }
    }
}

From source file:com.imag.nespros.gui.plugin.GraphEditor.java

private static void initMenu() {
    JMenu menu = new JMenu("File");
    menu.add(new AbstractAction("Make Image") {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            int option = chooser.showSaveDialog(demo);
            if (option == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                demo.writeJPEGImage(file);
            }/*from  w w  w .j av a  2 s  .  c  o  m*/
        }
    });
    menu.add(new AbstractAction("Print") {
        public void actionPerformed(ActionEvent e) {
            PrinterJob printJob = PrinterJob.getPrinterJob();
            printJob.setPrintable(demo);
            if (printJob.printDialog()) {
                try {
                    printJob.print();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    });
    menu.add(new AbstractAction("Save topology") {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            int option = chooser.showSaveDialog(demo);
            if (option == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                try {
                    demo.save(file);
                    frame.setTitle(file.getName());
                } catch (IOException ex) {
                    Logger.getLogger(GraphEditor.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    });
    menu.add(new AbstractAction("Load topology") {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            int option = chooser.showOpenDialog(demo);
            if (option == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                try {
                    //EPGraph.getInstance().resetMapping();
                    simu.resetMapping();
                    demo.load(file);
                    frame.setTitle("Simulator - " + file.getName());
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(GraphEditor.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    });
    JMenu menu2 = new JMenu("View");
    menu2.add(new AbstractAction("Layout") {
        @Override
        public void actionPerformed(ActionEvent e) {
            Layout l = new CircleLayout<Device, ComLink>(Topology.getInstance().getGraph());
            l.setInitializer(vv.getGraphLayout());
            l.setSize(vv.getSize());
            LayoutTransition<Device, ComLink> lt = new LayoutTransition<>(vv, vv.getGraphLayout(), l);
            Animator animator = new Animator(lt);
            animator.start();
            vv.getRenderContext().getMultiLayerTransformer().setToIdentity();
            vv.repaint();
        }
    });
    menu2.add(new AbstractAction("Event Composition Networks") {
        @Override
        public void actionPerformed(ActionEvent e) {
            showEPGraph(EPGraph.getInstance().getGraph());
        }
    });
    JPopupMenu.setDefaultLightWeightPopupEnabled(false);
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(menu);
    menuBar.add(menu2);
    frame.setJMenuBar(menuBar);
    frame.getContentPane().add(demo);
    frame.pack();
    frame.setVisible(true);
    buildEPGraphs();
    showEPGraph(EPGraph.getInstance().getGraph());
}