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

public static JMenu menu(String name, JMenuItem... items) {
    final JMenu menu = new JMenu(name);
    for (JMenuItem item : items) {
        if (item != null) {
            menu.add(item);
        } else {//from   www . ja va2 s  .  co  m
            menu.addSeparator();
        }
    }
    return menu;
}

From source file:components.GlassPaneDemo.java

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread./*from  w ww  . j  a v a 2 s .com*/
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("GlassPaneDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Start creating and adding components.
    JCheckBox changeButton = new JCheckBox("Glass pane \"visible\"");
    changeButton.setSelected(false);

    //Set up the content pane, where the "main GUI" lives.
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new FlowLayout());
    contentPane.add(changeButton);
    contentPane.add(new JButton("Button 1"));
    contentPane.add(new JButton("Button 2"));

    //Set up the menu bar, which appears above the content pane.
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Menu");
    menu.add(new JMenuItem("Do nothing"));
    menuBar.add(menu);
    frame.setJMenuBar(menuBar);

    //Set up the glass pane, which appears over both menu bar
    //and content pane and is an item listener on the change
    //button.
    myGlassPane = new MyGlassPane(changeButton, menuBar, frame.getContentPane());
    changeButton.addItemListener(myGlassPane);
    frame.setGlassPane(myGlassPane);

    //Show the window.
    frame.pack();
    frame.setVisible(true);
}

From source file:GlassPaneDemo.java

/**
 * Create the GUI and show it. For thread safety, this method should be
 * invoked from the event-dispatching thread.
 */// w w  w  .j av a2  s  . c om
private static void createAndShowGUI() {
    // Create and set up the window.
    JFrame frame = new JFrame("GlassPaneDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Start creating and adding components.
    JCheckBox changeButton = new JCheckBox("Glass pane \"visible\"");
    changeButton.setSelected(false);

    // Set up the content pane, where the "main GUI" lives.
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new FlowLayout());
    contentPane.add(changeButton);
    contentPane.add(new JButton("Button 1"));
    contentPane.add(new JButton("Button 2"));

    // Set up the menu bar, which appears above the content pane.
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Menu");
    menu.add(new JMenuItem("Do nothing"));
    menuBar.add(menu);
    frame.setJMenuBar(menuBar);

    // Set up the glass pane, which appears over both menu bar
    // and content pane and is an item listener on the change
    // button.
    myGlassPane = new MyGlassPane(changeButton, menuBar, frame.getContentPane());
    changeButton.addItemListener(myGlassPane);
    frame.setGlassPane(myGlassPane);

    // Show the window.
    frame.pack();
    frame.setVisible(true);
}

From source file:jgraph.JShow.java

/**
 * a driver for this demo/*w w w .  j a v  a2  s.  co m*/
 */
@SuppressWarnings("serial")
public static void showtest(DirectedOrderedSparseMultigraph<Object, Object> graph) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JShow demo = new JShow(graph);

    JMenu menu = new JMenu("Snapshot");
    menu.add(new AbstractAction("To JPEG") {
        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);
            }
        }
    });
    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();
                }
            }
        }
    });
    JPopupMenu.setDefaultLightWeightPopupEnabled(false);
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(menu);
    frame.setJMenuBar(menuBar);
    frame.getContentPane().add(demo);
    frame.pack();
    frame.setVisible(true);
}

From source file:ec.ui.view.AutoCorrelationsView.java

private static JMenu buildMenu(ChartPanel chartPanel) {
    JMenu result = new JMenu();

    result.add(MatrixChartCommand.copySeries(0, 0).toAction(chartPanel)).setText("Copy series");

    JMenu export = new JMenu("Export image to");
    export.add(ChartCommand.printImage().toAction(chartPanel)).setText("Printer...");
    export.add(ChartCommand.copyImage().toAction(chartPanel)).setText("Clipboard");
    export.add(ChartCommand.saveImage().toAction(chartPanel)).setText("File...");
    result.add(export);//from  w  w  w .  j a v  a  2  s.  co m

    return result;
}

From source file:org.jdal.swing.form.FormUtils.java

/**
 * Make a JMenu from an Action List /*w  w  w. jav a2 s .  com*/
 * @param actions the Action List
 * @return JMenu 
 */
public static JMenu toMenu(List<Action> actions) {
    JMenu menu = new JMenu();

    for (Action a : actions)
        menu.add(a);

    return menu;
}

From source file:de.javagl.jgltf.browser.MenuNodes.java

/**
 * Create a list of menu items (which may be JMenu instances) for the 
 * given {@link MenuNode} instances.  //from w w w .  ja  v a 2 s  . c  o m
 * 
 * @param menuNodes The {@link MenuNode}s
 * @return The menus
 */
private static List<JMenuItem> createMenuItems(List<? extends MenuNode> menuNodes) {
    List<JMenuItem> menuItems = new ArrayList<JMenuItem>();

    for (MenuNode menuNode : menuNodes) {
        if (menuNode.children != null) {
            JMenu menu = new JMenu();
            menu.setText(menuNode.label);

            List<JMenuItem> childMenuItems = createMenuItems(menuNode.children);
            for (JMenuItem childMenuItem : childMenuItems) {
                menu.add(childMenuItem);
            }
            menuItems.add(menu);
        } else {
            if (menuNode.command == null) {
                logger.warning("Empty menu node - skipping");
                continue;
            }
            JMenuItem menuItem = new JMenuItem();
            String label = "<html>" + menuNode.label + " <font size=-2>(" + menuNode.command + ")</font>"
                    + "</html>";
            menuItem.setText(label);
            menuItem.setActionCommand(menuNode.command);
            menuItems.add(menuItem);
        }

    }
    return menuItems;
}

From source file:Main.java

/**
 * Creates a copy of this menu item, whose contents update automatically
 * whenever the original menu item changes.
 *///from   w ww .  j a  v  a 2s.co  m
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;
}

From source file:ec.ui.view.DistributionView.java

private static JMenu buildMenu(ChartPanel chartPanel) {
    JMenu result = new JMenu();

    result.add(MatrixChartCommand.copySeries(DISTRIBUTION_INDEX, 0).toAction(chartPanel))
            .setText("Copy distribution");
    result.add(MatrixChartCommand.copySeries(HISTOGRAM_INDEX, 0).toAction(chartPanel))
            .setText("Copy histogram");

    JMenu export = new JMenu("Export image to");
    export.add(ChartCommand.printImage().toAction(chartPanel)).setText("Printer...");
    export.add(ChartCommand.copyImage().toAction(chartPanel)).setText("Clipboard");
    export.add(ChartCommand.saveImage().toAction(chartPanel)).setText("File...");
    result.add(export);//from w ww  . j  av a  2 s .  co  m

    return result;
}

From source file:Main.java

/**
 * Creates a sub-menu for changing Look'n'Feel.
 * @param rootComponent the root component which Look'n'Feel should be changed (child component are processed recursively).
 * @return a menu item with sub-menu items for each Look'n'Feel
 * installed in the system with associated actions to change the
 * Look'n'Feel for the <code>rootComponent</root>.
 *//*  ww w  .j  a va 2s  .c o  m*/
public static JMenu getLafMenu(final Component rootComponent) {
    JMenu jMenu = new JMenu("Look & Feel");
    ButtonGroup buttonGroup = new ButtonGroup();
    final UIManager.LookAndFeelInfo[] installedLFs = UIManager.getInstalledLookAndFeels();
    String currentLF = UIManager.getLookAndFeel().getName();
    for (int i = 0; i < installedLFs.length; i++) {
        JCheckBoxMenuItem jMenuItem = new JCheckBoxMenuItem(installedLFs[i].getName());
        jMenu.add(jMenuItem);
        buttonGroup.add(jMenuItem);
        jMenuItem.setState(currentLF.equals(installedLFs[i].getName()));
        class ChangeLF extends AbstractAction {
            private UIManager.LookAndFeelInfo iLF;

            public ChangeLF(UIManager.LookAndFeelInfo iLF) {
                super(iLF.getName());
                this.iLF = iLF;
            }

            public void actionPerformed(ActionEvent e) {
                try {
                    UIManager.setLookAndFeel(iLF.getClassName());
                    SwingUtilities.updateComponentTreeUI(rootComponent);
                } catch (Exception ex) {
                    System.out.print("Could not set look and feel: " + ex.toString());
                }
            }
        }
        jMenuItem.setAction(new ChangeLF(installedLFs[i]));
    }
    return jMenu;
}