Example usage for javax.swing JMenuItem JMenuItem

List of usage examples for javax.swing JMenuItem JMenuItem

Introduction

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

Prototype

public JMenuItem(Action a) 

Source Link

Document

Creates a menu item whose properties are taken from the specified Action.

Usage

From source file:com.sshtools.appframework.api.ui.ActionBuilder.java

protected void rebuildMenuBar(Collection<AppAction> enabledActions) {
    menuBar.invalidate();// ww  w .ja v  a2  s  .c om

    // Build the menu bar action list
    menuBar.removeAll();
    List<AppAction> menuBarActions = new ArrayList<AppAction>();
    for (AppAction action : enabledActions) {
        if (Boolean.TRUE.equals(action.getValue(AppAction.ON_MENUBAR))) {
            menuBarActions.add(action);
        }
    }
    log.debug("There are " + menuBarActions.size() + " on the menubar");

    // Build the menu bar
    List<ActionMenu> menus = new ArrayList<ActionMenu>(listActionMenus());
    Collections.sort(menus);
    Map<String, List<AppAction>> map = new HashMap<String, List<AppAction>>();
    for (AppAction z : menuBarActions) {
        String menuName = (String) z.getValue(AppAction.MENU_NAME);
        if (menuName == null) {
        } else {
            ;
            String m = (String) z.getValue(AppAction.MENU_NAME);
            ActionMenu menu = getActionMenu(menus.iterator(), m);
            if (menu != null) {
                List<AppAction> x = map.get(menu.getName());
                if (x == null) {
                    x = new ArrayList<AppAction>();
                    map.put(menu.getName(), x);
                }
                x.add(z);
            }
        }
    }

    // Create the menu components
    for (ActionMenu m : menus) {
        List<AppAction> x = map.get(m.getName());
        if (x != null) {
            Collections.sort(x, new MenuItemActionComparator());
            JMenu menu = new JMenu(m.getDisplayName());
            menu.setMnemonic(m.getWeight());
            Integer grp = null;
            for (AppAction a : x) {
                Integer g = (Integer) a.getValue(AppAction.MENU_ITEM_GROUP);
                if ((grp != null) && !g.equals(grp)) {
                    menu.addSeparator();
                }
                grp = g;
                if (a instanceof MenuAction) {
                    JMenu mnu = (JMenu) a.getValue(MenuAction.MENU);
                    menu.add(mnu);
                } else {
                    if (Boolean.TRUE.equals(a.getValue(AppAction.IS_TOGGLE_BUTTON))) {
                        menu.add(new ActionJCheckboxMenuItem(a));
                    } else {
                        JMenuItem item = new JMenuItem(a);
                        menu.add(item);
                    }
                }
            }
            menuBar.add(menu);
        }
    }
    menuBar.validate();
    menuBar.repaint();
}

From source file:de.tntinteractive.portalsammler.gui.MainDialog.java

private JPopupMenu createConfigMenu() {
    final JPopupMenu menu = new JPopupMenu();

    final JMenuItem sourceConfig = new JMenuItem("Quellen verwalten...");
    sourceConfig.addActionListener(new ActionListener() {
        @Override//from   www  . j a  va2s. co m
        public void actionPerformed(final ActionEvent e) {
            MainDialog.this.gui.showConfigGui(MainDialog.this.getStore());
        }
    });
    menu.add(sourceConfig);

    final JMenuItem changePassword = new JMenuItem("Neues Passwort...");
    changePassword.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            try {
                MainDialog.this.changePassword();
            } catch (final GeneralSecurityException ex) {
                MainDialog.this.gui.showError(ex);
            } catch (final IOException ex) {
                MainDialog.this.gui.showError(ex);
            }
        }
    });
    menu.add(changePassword);

    return menu;
}

From source file:MenuTest.java

public MenuFrame() {
    setTitle("MenuTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    JMenu fileMenu = new JMenu("File");
    fileMenu.add(new TestAction("New"));

    // demonstrate accelerators

    JMenuItem openItem = fileMenu.add(new TestAction("Open"));
    openItem.setAccelerator(KeyStroke.getKeyStroke("ctrl O"));

    fileMenu.addSeparator();//from w  w w  .jav a  2s  .c o  m

    saveAction = new TestAction("Save");
    JMenuItem saveItem = fileMenu.add(saveAction);
    saveItem.setAccelerator(KeyStroke.getKeyStroke("ctrl S"));

    saveAsAction = new TestAction("Save As");
    fileMenu.add(saveAsAction);
    fileMenu.addSeparator();

    fileMenu.add(new AbstractAction("Exit") {
        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }
    });

    // demonstrate check box and radio button menus

    readonlyItem = new JCheckBoxMenuItem("Read-only");
    readonlyItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            boolean saveOk = !readonlyItem.isSelected();
            saveAction.setEnabled(saveOk);
            saveAsAction.setEnabled(saveOk);
        }
    });

    ButtonGroup group = new ButtonGroup();

    JRadioButtonMenuItem insertItem = new JRadioButtonMenuItem("Insert");
    insertItem.setSelected(true);
    JRadioButtonMenuItem overtypeItem = new JRadioButtonMenuItem("Overtype");

    group.add(insertItem);
    group.add(overtypeItem);

    // demonstrate icons

    Action cutAction = new TestAction("Cut");
    cutAction.putValue(Action.SMALL_ICON, new ImageIcon("cut.gif"));
    Action copyAction = new TestAction("Copy");
    copyAction.putValue(Action.SMALL_ICON, new ImageIcon("copy.gif"));
    Action pasteAction = new TestAction("Paste");
    pasteAction.putValue(Action.SMALL_ICON, new ImageIcon("paste.gif"));

    JMenu editMenu = new JMenu("Edit");
    editMenu.add(cutAction);
    editMenu.add(copyAction);
    editMenu.add(pasteAction);

    // demonstrate nested menus

    JMenu optionMenu = new JMenu("Options");

    optionMenu.add(readonlyItem);
    optionMenu.addSeparator();
    optionMenu.add(insertItem);
    optionMenu.add(overtypeItem);

    editMenu.addSeparator();
    editMenu.add(optionMenu);

    // demonstrate mnemonics

    JMenu helpMenu = new JMenu("Help");
    helpMenu.setMnemonic('H');

    JMenuItem indexItem = new JMenuItem("Index");
    indexItem.setMnemonic('I');
    helpMenu.add(indexItem);

    // you can also add the mnemonic key to an action
    Action aboutAction = new TestAction("About");
    aboutAction.putValue(Action.MNEMONIC_KEY, new Integer('A'));
    helpMenu.add(aboutAction);

    // add all top-level menus to menu bar

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    menuBar.add(fileMenu);
    menuBar.add(editMenu);
    menuBar.add(helpMenu);

    // demonstrate pop-ups

    popup = new JPopupMenu();
    popup.add(cutAction);
    popup.add(copyAction);
    popup.add(pasteAction);

    JPanel panel = new JPanel();
    panel.setComponentPopupMenu(popup);
    add(panel);

    // the following line is a workaround for bug 4966109
    panel.addMouseListener(new MouseAdapter() {
    });
}

From source file:org.fhcrc.cpl.toolbox.gui.chart.PanelWithChart.java

/**
 * Add two new menu items to the popup menu, for saving to TSV and CSV files
 *//*from  w  ww.j a  va  2  s  . co m*/
protected void initPopupMenu() {
    addSeparatorToPopupMenu();

    //TSV
    JMenuItem saveTSVMenuItem = new JMenuItem(TextProvider.getText("SAVE_DATA_AS_TSV"));
    saveTSVMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            JFileChooser fc = new JFileChooser();
            int chooserStatus = fc.showOpenDialog(PanelWithChart.this);
            //if user didn't hit OK, ignore
            if (chooserStatus != JFileChooser.APPROVE_OPTION)
                return;

            File outFile = fc.getSelectedFile();
            saveChartDataToTSV(outFile);
        }
    });
    addItemToPopupMenu(saveTSVMenuItem);

    //CSV
    JMenuItem saveCSVMenuItem = new JMenuItem(TextProvider.getText("SAVE_DATA_AS_CSV"));
    saveCSVMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            JFileChooser wfc = new JFileChooser();
            int chooserStatus = wfc.showOpenDialog(PanelWithChart.this);
            //if user didn't hit OK, ignore
            if (chooserStatus != JFileChooser.APPROVE_OPTION)
                return;

            File outFile = wfc.getSelectedFile();
            saveChartDataToCSV(outFile);
        }
    });
    addItemToPopupMenu(saveCSVMenuItem);
}

From source file:dmh.kuebiko.view.NoteStackFrame.java

/**
 * Create the frame./*from   w w  w  .ja  va2  s  .  c  o  m*/
 */
public NoteStackFrame(NoteManager noteMngr) {
    this.noteMngr = noteMngr;

    // Setup the various actions for the frame.
    noteMngr.addObserver(new Observer() {
        @Override
        public void update(Observable o, Object arg) {
            boolean stackChanged = (Boolean) arg;
            toggleUnsavedChangeIndicator(stackChanged);
            if (!stackChanged) {
                notePanel.getHuxleyUiManager().resetTextChanged();
            }
        }
    });

    ActionObserverUtil.registerEnMass(actionMngr, observable, new NewNoteAction(this), new OpenNoteAction(this),
            new DeleteNoteAction(this), new RenameNoteAction(this), new SaveStackAction(this));

    // Build the menus.
    menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    fileMenu = new JMenu("File");
    menuBar.add(fileMenu);

    newNoteMenuItem = new JMenuItem(actionMngr.getAction(NewNoteAction.class));
    fileMenu.add(newNoteMenuItem);

    JMenuItem openNoteMenuItem = new JMenuItem(actionMngr.getAction(OpenNoteAction.class));
    fileMenu.add(openNoteMenuItem);

    deleteNoteMenuItem = new JMenuItem(actionMngr.getAction(DeleteNoteAction.class));
    fileMenu.add(deleteNoteMenuItem);

    renameNoteMenuItem = new JMenuItem(actionMngr.getAction(RenameNoteAction.class));
    fileMenu.add(renameNoteMenuItem);

    fileMenu.addSeparator();

    newStackMenuItem = new JMenuItem(actionMngr.getAction(NewStackAction.class));
    fileMenu.add(newStackMenuItem);

    openStackMenuItem = new JMenuItem(actionMngr.getAction(OpenStackAction.class));
    fileMenu.add(openStackMenuItem);

    fileMenu.addSeparator();

    closeMenuItem = new JMenuItem("Close");
    closeMenuItem.setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_W, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    closeMenuItem.setEnabled(false);
    fileMenu.add(closeMenuItem);

    closeAllMenuItem = new JMenuItem("Close All");
    closeAllMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,
            InputEvent.SHIFT_MASK | Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    closeAllMenuItem.setEnabled(false);
    fileMenu.add(closeAllMenuItem);

    fileMenu.addSeparator();

    saveMenuItem = new JMenuItem(actionMngr.getAction(SaveStackAction.class));
    fileMenu.add(saveMenuItem);

    saveAllMenuItem = new JMenuItem("Save All");
    saveAllMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
            InputEvent.SHIFT_MASK | Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    saveAllMenuItem.setEnabled(false);
    fileMenu.add(saveAllMenuItem);

    editMenu = new JMenu("Edit");
    menuBar.add(editMenu);

    undoMenuItem = new JMenuItem("Undo");
    undoMenuItem.setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_Z, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    editMenu.add(undoMenuItem);

    redoMenuItem = new JMenuItem("Redo"); // FIXME mac-specific keyboard shortcut
    redoMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,
            InputEvent.SHIFT_MASK | Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    editMenu.add(redoMenuItem);

    editMenu.addSeparator();

    cutMenuItem = new JMenuItem(actionMngr.getAction(DefaultEditorKit.CutAction.class));
    cutMenuItem.setText("Cut");
    cutMenuItem.setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_X, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    editMenu.add(cutMenuItem);

    copyMenuItem = new JMenuItem(actionMngr.getAction(DefaultEditorKit.CopyAction.class));
    copyMenuItem.setText("Copy");
    copyMenuItem.setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_C, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    editMenu.add(copyMenuItem);

    pasteMenuItem = new JMenuItem(actionMngr.getAction(DefaultEditorKit.PasteAction.class));
    pasteMenuItem.setText("Paste");
    pasteMenuItem.setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_V, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    editMenu.add(pasteMenuItem);

    textMenu = new JMenu("Text");
    menuBar.add(textMenu);

    JMenu windowMenu = new JMenu("Window");
    menuBar.add(windowMenu);

    initialize();
    additionalSetup();
}

From source file:unikn.dbis.univis.visualization.graph.plaf.VGraphUI.java

/**
 * Creates the listener responsible for calling the correct handlers based
 * on mouse events, and to select invidual cells.
 *//*w w w  . ja  v  a  2  s.  co m*/
protected MouseListener createMouseListener() {

    return new MouseHandler() {

        /**
         * Invoked when a mouse button has been pressed on a component.
         *
         //@Override
         public void mousePressed(MouseEvent e) {
                
         Object o = graph.getFirstCellForLocation(e.getX(), e.getY());
                
         if (o instanceof VGraphCell) {
                
         VGraphCell cell = (VGraphCell) o;
                
         JPopupMenu menu = new JPopupMenu();
                
         VChartPanel chartPanel = (VChartPanel) cell.getUserObject();
                
         if (chartPanel.isShowPopUp()) {
                
         LegendItemCollection collect = chartPanel.getChart().getPlot().getLegendItems();
         JMenu first = new JMenu("1-39");
         int checker = 0;
                
         for (Iterator iter = collect.iterator(); iter.hasNext();) {
         LegendItem item = (LegendItem) iter.next();
         checker++;
         first.add(new JMenuItem(item.getLabel()));
         if ((checker % 40) == 0) {
         menu.add(first);
                
         first = new JMenu("" + checker + "-" + (checker + 39));
         }
         if (!iter.hasNext()) {
         menu.add(first);
         }
         }
                
         menu.show(graph, e.getX(), e.getY());
         }
         }
                
         super.mousePressed(e);
         }
         */

        /**
         * Invoked when a mouse button has been pressed on a component.
         */
        @Override
        public void mousePressed(MouseEvent e) {

            Object o = graph.getFirstCellForLocation(e.getX(), e.getY());

            if (SwingUtilities.isLeftMouseButton(e) && e.isAltDown()) {
                if (o instanceof VGraphCell) {

                    VGraphCell cell = (VGraphCell) o;

                    JPopupMenu menu = new JPopupMenu();

                    VChartPanel chartPanel = (VChartPanel) cell.getUserObject();

                    LegendItemCollection collect = chartPanel.getChart().getPlot().getLegendItems();
                    JMenu first = new JMenu("1-39");
                    int checker = 0;

                    for (Iterator iter = collect.iterator(); iter.hasNext();) {
                        LegendItem item = (LegendItem) iter.next();
                        checker++;
                        first.add(new JMenuItem(item.getLabel()));
                        if ((checker % 40) == 0) {
                            menu.add(first);

                            first = new JMenu("" + checker + "-" + (checker + 39));
                        }
                        if (!iter.hasNext()) {
                            menu.add(first);
                        }
                    }

                    menu.show(graph, e.getX(), e.getY());
                }
            }

            super.mousePressed(e);

            if (o != null && o instanceof VGraphCell) {

                VGraphCell cell = (VGraphCell) o;

                o = cell.getUserObject();

                if (o != null && o instanceof VChartPanel) {
                    VChartPanel chart = (VChartPanel) o;

                    for (MouseListener l : chart.getMouseListeners()) {
                        l.mousePressed(e);
                    }
                }
            }
        }

        // Event may be null when called to cancel the current operation.
        @Override
        public void mouseReleased(MouseEvent e) {
            super.mouseReleased(e);

            Object o = graph.getFirstCellForLocation(e.getX(), e.getY());

            if (o != null && o instanceof VGraphCell) {

                VGraphCell cell = (VGraphCell) o;

                o = cell.getUserObject();

                if (o != null && o instanceof VChartPanel) {
                    VChartPanel chart = (VChartPanel) o;

                    for (MouseListener l : chart.getMouseListeners()) {
                        l.mouseReleased(e);
                    }
                }
            }
        }

        /**
         * Invoked when the mouse has been clicked on a component.
         */
        @Override
        public void mouseClicked(MouseEvent e) {
            super.mouseClicked(e);

            Object o = graph.getFirstCellForLocation(e.getX(), e.getY());

            if (o != null && o instanceof VGraphCell) {

                VGraphCell cell = (VGraphCell) o;

                o = cell.getUserObject();

                if (o != null && o instanceof VChartPanel) {
                    VChartPanel chart = (VChartPanel) o;

                    if (SwingUtilities.isRightMouseButton(e)) {
                        JPopupMenu menu = chart.createPopupMenu(true, true, true, true);

                        menu.show(graph, e.getX(), e.getY());
                    }

                    /*
                    for (MouseListener l : chart.getMouseListeners()) {
                    System.out.println("LISTENS CLI");
                    l.mouseClicked(e);
                    }
                    */
                }
            }
        }

        /*
        // Event may be null when called to cancel the current operation.
        @Override
        public void mouseReleased(MouseEvent e) {
        super.mouseReleased(e);
                
        Object[] cells = graphSelectionModel.getSelectionCells();
                
        Rectangle2D bounds = graph.getCellBounds(cells);
                
        if (bounds != null) {
            Rectangle2D b2 = graph.toScreen((Rectangle2D) bounds.clone());
            graph.scrollRectToVisible(new Rectangle((int) b2.getX(), (int) b2.getY(), (int) b2.getWidth(), (int) b2.getHeight()));
        }
        }
        */

        /**
         * Invoked when the mouse pointer has been moved on a component (with no
         * buttons down).
         */
        @Override
        public void mouseMoved(MouseEvent e) {

            if (graph.isMoveable()) {
                super.mouseMoved(e);
            }

            Object o = graph.getFirstCellForLocation(e.getX(), e.getY());

            if (o != null && o instanceof VGraphCell) {

                selectedCell = (VGraphCell) o;

                Rectangle2D bounds = graph.getCellBounds(selectedCell);

                menu.show(graph, (int) (bounds.getX() + bounds.getWidth()),
                        (int) bounds.getY() + (int) (bounds.getHeight() - menu.getHeight()));
            }
        }
    };
}

From source file:net.pandoragames.far.ui.swing.menu.FileMenu.java

private void init(final Localizer localizer, final ComponentRepository componentRepository) {
    //   Import/* ww w.jav a2 s . c  o m*/
    JMenuItem importMenu = new JMenuItem(localizer.localize("label.import"));
    importMenu.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser fileChooser = new JFileChooser(config.getFileListExportDirectory());
            fileChooser.setDialogTitle(localizer.localize("label.select-import-file"));
            fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            int returnVal = fileChooser.showOpenDialog(FileMenu.this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                Runnable fileImporter = new ImportAction(fileChooser.getSelectedFile(), localizer,
                        componentRepository.getOperationCallBackListener(), config.isWindows());
                Thread thread = new Thread(fileImporter);
                thread.setDaemon(true);
                thread.start();
            }
        }
    });
    this.add(importMenu);
    //   Export
    export = new JMenuItem(localizer.localize("label.export"));
    export.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ExportFileListDialog dialog = new ExportFileListDialog(mainFrame, tableModel, config);
            dialog.pack();
            dialog.setVisible(true);
        }
    });
    this.add(export);
    // seperator 
    this.addSeparator();
    //   Edit
    edit = new JMenuItem(localizer.localize("label.edit"));
    edit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            FileEditor editor = new FileEditor(mainFrame, tableModel.getSelectedRows().get(0), config);
            editor.pack();
            editor.setVisible(true);
        }
    });
    this.add(edit);
    //   View
    JMenuItem view = new JMenuItem(viewAction);
    this.add(view);
    //   Preview
    JMenuItem preview = new JMenuItem(previewAction);
    this.add(preview);
    //   Info
    info = new JMenuItem(localizer.localize("label.info"));
    info.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            InfoView infoView = new InfoView(mainFrame, tableModel.getSelectedRows().get(0), config,
                    repository);
            infoView.pack();
            infoView.setVisible(true);
        }
    });
    this.add(info);
    // seperator 
    this.addSeparator();
    // copy
    copy = new JMenuItem(localizer.localize("menu.copy"));
    copy.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            FileOperationDialog.copyDialog(tableModel, findForm, errorSink, config, mainFrame);
        }
    });
    this.add(copy);
    // tree copy
    treeCopy = new JMenuItem(localizer.localize("menu.treeCopy"));
    treeCopy.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            FileOperationDialog.treeCopyDialog(tableModel, findForm, errorSink, config, mainFrame);
        }
    });
    this.add(treeCopy);
    // move
    move = new JMenuItem(localizer.localize("menu.move"));
    move.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            FileOperationDialog.moveDialog(tableModel, findForm, errorSink, config, mainFrame);
        }
    });
    this.add(move);
    // delete
    delete = new JMenuItem(localizer.localize("menu.delete"));
    delete.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            FileOperationDialog.deleteDialog(tableModel, findForm.getBaseDirectory(), errorSink, config,
                    mainFrame);
        }
    });
    this.add(delete);
    // rename
    rename = new JMenuItem(localizer.localize("menu.rename-dialog"));
    rename.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int rowIndex = tableModel.getRowIndex(tableModel.getSelectedRows().get(0));
            FileOperationDialog.renameDialog(rowIndex, tableModel, findForm, errorSink, config, mainFrame);
        }
    });
    this.add(rename);
}

From source file:instance.gui.InstanceGUI.java

private void createRestartMenuItem() {

    restartMenuItem = new JMenuItem("Restart Instance");
    restartMenuItem.addActionListener(restartActionListener);

    fileMenu.add(restartMenuItem);/*from w w  w  .ja v a 2s .  c  om*/
}

From source file:misc.ActionDemo.java

public JMenuBar createMenuBar() {
    JMenuItem menuItem = null;/*  w w  w . j a v a 2 s. c  om*/
    JMenuBar menuBar;

    //Create the menu bar.
    menuBar = new JMenuBar();

    //Create the first menu.
    JMenu mainMenu = new JMenu("Menu");

    Action[] actions = { leftAction, middleAction, rightAction };
    for (int i = 0; i < actions.length; i++) {
        menuItem = new JMenuItem(actions[i]);
        menuItem.setIcon(null); //arbitrarily chose not to use icon
        mainMenu.add(menuItem);
    }

    //Set up the menu bar.
    menuBar.add(mainMenu);
    menuBar.add(createAbleMenu());
    return menuBar;
}

From source file:net.adamjak.thomas.graph.application.gui.ResultsWidnow.java

private JMenuBar createJMenuBar() {
    this.jMenuBar = new JMenuBar();

    // Menu File/*from  w ww . j av  a2s  .  co  m*/
    JMenu jmFile = new JMenu("File");

    JMenuItem jmiFileSaveResults = new JMenuItem("Save results");
    jmiFileSaveResults.setAccelerator(GuiAccelerators.CTRL_S);
    jmiFileSaveResults.addActionListener(new AlJmiFileSaveResults());

    jmFile.add(jmiFileSaveResults);

    //jmFile.addSeparator();

    this.jMenuBar.add(jmFile);

    return this.jMenuBar;
}