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 void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final Action printAction = new PrintHelloAction();

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("File");
    menuBar.add(menu);// w ww  .j  a v a  2s .  c o  m
    JMenuItem menuItem = new JMenuItem("Print");
    KeyStroke ctrlP = KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK);
    menuItem.setAccelerator(ctrlP);
    menuItem.addActionListener(printAction);
    menu.add(menuItem);

    JButton fileButton = new JButton("About");
    fileButton.setMnemonic(KeyEvent.VK_A);
    fileButton.addActionListener(printAction);

    frame.setJMenuBar(menuBar);

    frame.add(fileButton, BorderLayout.SOUTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:ScreenDump.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Action Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final Action printAction = new PrintHelloAction();

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("File");
    menuBar.add(menu);/*from ww  w.  j a va2s. c o  m*/
    JMenuItem menuItem = new JMenuItem("Print");
    KeyStroke ctrlP = KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK);
    menuItem.setAccelerator(ctrlP);
    menuItem.addActionListener(printAction);
    menu.add(menuItem);

    JButton fileButton = new JButton("About");
    fileButton.setMnemonic(KeyEvent.VK_A);
    fileButton.addActionListener(printAction);

    frame.setJMenuBar(menuBar);

    Container contentPane = frame.getContentPane();
    contentPane.add(fileButton, BorderLayout.SOUTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Mnemonic/Accelerator Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final Action printAction = new PrintHelloAction();

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("File");
    menuBar.add(menu);/*from   ww  w  .  ja v  a  2  s . co  m*/
    JMenuItem menuItem = new JMenuItem("Print");
    KeyStroke ctrlP = KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK);
    menuItem.setAccelerator(ctrlP);
    menuItem.addActionListener(printAction);
    menu.add(menuItem);

    JButton fileButton = new JButton("About");
    fileButton.setMnemonic(KeyEvent.VK_A);
    fileButton.addActionListener(printAction);

    frame.setJMenuBar(menuBar);

    frame.add(fileButton, BorderLayout.SOUTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:SwingToolBarSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Action Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Action showAction = new ShowAction(frame);
    JMenuBar menuBar = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic('f');
    JMenuItem newMenuItem = new JMenuItem("New", 'N');
    fileMenu.add(newMenuItem);
    JMenuItem openMenuItem = new JMenuItem("Open", 'O');
    fileMenu.add(openMenuItem);/*from  w  w  w . j  a  v  a2 s  . c  o m*/
    JMenuItem closeMenuItem = new JMenuItem("Close", 'C');
    fileMenu.add(closeMenuItem);
    fileMenu.addSeparator();
    JMenuItem saveMenuItem = new JMenuItem("Save", 'S');
    fileMenu.add(saveMenuItem);
    fileMenu.add(showAction);
    fileMenu.addSeparator();
    JMenuItem exitMenuItem = new JMenuItem("Exit", 'X');
    fileMenu.add(exitMenuItem);
    menuBar.add(fileMenu);
    JMenu editMenu = new JMenu("Edit");
    JMenuItem cutMenuItem = new JMenuItem("Cut", 'T');
    KeyStroke ctrlXKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK);
    cutMenuItem.setAccelerator(ctrlXKeyStroke);
    editMenu.add(cutMenuItem);
    JMenuItem copyMenuItem = new JMenuItem("Copy", 'C');
    KeyStroke ctrlCKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK);
    copyMenuItem.setAccelerator(ctrlCKeyStroke);
    editMenu.add(copyMenuItem);
    JMenuItem pasteMenuItem = new JMenuItem("Paste", 'P');
    KeyStroke ctrlVKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK);
    pasteMenuItem.setAccelerator(ctrlVKeyStroke);
    pasteMenuItem.setEnabled(false);
    editMenu.add(pasteMenuItem);
    editMenu.addSeparator();
    JMenuItem findMenuItem = new JMenuItem("Find", 'F');
    KeyStroke f3KeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0);
    findMenuItem.setAccelerator(f3KeyStroke);
    editMenu.add(findMenuItem);
    editMenu.setMnemonic('e');
    editMenu.add(showAction);
    menuBar.add(editMenu);
    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:GraphEditorDemo.java

/**
 * a driver for this demo/*from   w  w  w  .j  a  va 2  s .  c om*/
 */
@SuppressWarnings("serial")
public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final GraphEditorDemo demo = new GraphEditorDemo();

    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);
            }
        }
    });
    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") {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            int option = chooser.showSaveDialog(demo);
            if (option == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                demo.writeTopologyFile(file);
            }
        }
    });
    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:JToggleButtonMenuItem.java

public static void main(final String args[]) {
    JFrame frame = new JFrame("MenuSample Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();

    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(fileMenu);/*from  w w w. j av a  2 s  . c  om*/

    // File->New, N - Mnemonic
    JToggleButtonMenuItem newMenuItem = new JToggleButtonMenuItem("New");
    fileMenu.add(newMenuItem);

    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:ToggleSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("JToggleButtonMenuItem Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JMenuBar bar = new JMenuBar();
    JMenu file = new JMenu("File");
    file.setMnemonic('f');
    JMenuItem newItem = new JMenuItem("New", 'N');
    file.add(newItem);
    JMenuItem openItem = new JMenuItem("Open", 'O');
    file.add(openItem);//from   ww  w  .  j a  v a 2s . co m
    JMenuItem closeItem = new JMenuItem("Close", 'C');
    file.add(closeItem);
    file.addSeparator();
    JMenuItem saveItem = new JMenuItem("Save", 'S');
    file.add(saveItem);
    file.addSeparator();
    JMenuItem exitItem = new JMenuItem("Exit", 'X');
    file.add(exitItem);
    bar.add(file);
    JMenu edit = new JMenu("Edit");
    JMenuItem cutItem = new JMenuItem("Cut", 'T');
    cutItem.setAccelerator(KeyStroke.getKeyStroke('X', Event.CTRL_MASK));
    edit.add(cutItem);
    JMenuItem copyItem = new JMenuItem("Copy", 'C');
    copyItem.setAccelerator(KeyStroke.getKeyStroke('C', Event.CTRL_MASK));
    edit.add(copyItem);
    JMenuItem pasteItem = new JMenuItem("Paste", 'P');
    pasteItem.setAccelerator(KeyStroke.getKeyStroke('V', Event.CTRL_MASK));
    pasteItem.setEnabled(false);
    edit.add(pasteItem);
    edit.addSeparator();
    JMenuItem findItem = new JMenuItem("Find", 'F');
    findItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0));
    edit.add(findItem);
    edit.setMnemonic('e');
    Icon atIcon = new ImageIcon("at.gif");
    JMenu findOptions = new JMenu("Options");
    findOptions.setIcon(atIcon);
    findOptions.setMnemonic('O');
    ButtonGroup directionGroup = new ButtonGroup();
    JRadioButtonMenuItem forward = new JRadioButtonMenuItem("Forward", true);
    findOptions.add(forward);
    directionGroup.add(forward);
    JRadioButtonMenuItem backward = new JRadioButtonMenuItem("Backward");
    findOptions.add(backward);
    directionGroup.add(backward);
    findOptions.addSeparator();
    JCheckBoxMenuItem caseItem = new JCheckBoxMenuItem("Case Insensitive");
    findOptions.add(caseItem);
    edit.add(findOptions);
    JToggleButtonMenuItem toggleItem = new JToggleButtonMenuItem("Ballon Help");
    toggleItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Selected");
        }
    });
    edit.add(toggleItem);
    bar.add(edit);
    frame.setJMenuBar(bar);
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:ActionsMenuBar.java

public static void main(String args[]) {
    final JFrame frame = new JFrame("TextAction Usage");
    Container contentPane = frame.getContentPane();
    final JScrollPane scrollPane = new JScrollPane();
    contentPane.add(scrollPane, BorderLayout.CENTER);

    final JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);/*  ww w.  j a v  a  2  s  .  c o m*/

    ActionListener actionListener = new ActionListener() {
        JTextComponent component;

        public void actionPerformed(ActionEvent actionEvent) {
            // Determine which component selected
            String command = actionEvent.getActionCommand();
            if (command.equals("JTextField")) {
                component = new JTextField();
            } else if (command.equals("JPasswordField")) {
                component = new JPasswordField();
            } else if (command.equals("JTextArea")) {
                component = new JTextArea();
            } else if (command.equals("JTextPane")) {
                component = new JTextPane();
            } else {
                component = new JEditorPane();
            }
            scrollPane.setViewportView(component);
            // Process action list
            Action actions[] = component.getActions();
            menuBar.removeAll();
            menuBar.revalidate();
            JMenu menu = null;
            for (int i = 0, n = actions.length; i < n; i++) {
                if ((i % 10) == 0) {
                    menu = new JMenu("From " + i);
                    menuBar.add(menu);
                }
                menu.add(actions[i]);
            }
            menuBar.revalidate();
        }
    };

    String components[] = { "JTextField", "JPasswordField", "JTextArea", "JTextPane", "JEditorPane" };
    final Container componentsContainer = RadioButtonUtils.createRadioButtonGrouping(components,
            "Pick to List Actions", actionListener);
    contentPane.add(componentsContainer, BorderLayout.WEST);

    frame.setSize(400, 300);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(final String args[]) {
    JFrame frame = new JFrame("MenuSample Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();

    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(fileMenu);//from w w w  .j  a v  a  2 s  .c  o m

    fileMenu.addChangeListener(new ChangeListener() {

        public void stateChanged(ChangeEvent e) {
            System.out.println("File Menu Changed");

        }
    });

    // File->New, N - Mnemonic
    JMenuItem newMenuItem = new JMenuItem("New");
    fileMenu.add(newMenuItem);

    newMenuItem.addChangeListener(new ChangeListener() {

        public void stateChanged(ChangeEvent e) {
            System.out.println("new menu item changed");

        }
    });

    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:RadioButtonSample.java

public static void main(String args[]) {

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton aButton = (AbstractButton) actionEvent.getSource();
            boolean selected = aButton.getModel().isSelected();
            System.out.println(actionEvent.getActionCommand() + " - selected? " + selected);
        }/*from w  w  w  .j a  v  a 2 s .  c  o  m*/
    };

    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent itemEvent) {
            AbstractButton aButton = (AbstractButton) itemEvent.getSource();
            int state = itemEvent.getStateChange();
            String selected = ((state == ItemEvent.SELECTED) ? "selected" : "not selected");
            System.out.println(aButton.getText() + " - selected? " + selected);
        }
    };

    JFrame frame = new JFrame("Radio Menu Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Menu");
    ButtonGroup buttonGroup = new ButtonGroup();
    menu.setMnemonic(KeyEvent.VK_M);

    JRadioButtonMenuItem emptyMenuItem = new JRadioButtonMenuItem();
    emptyMenuItem.setActionCommand("Empty");
    emptyMenuItem.addActionListener(actionListener);
    buttonGroup.add(emptyMenuItem);
    menu.add(emptyMenuItem);

    JRadioButtonMenuItem oneMenuItem = new JRadioButtonMenuItem("Partridge");
    oneMenuItem.addActionListener(actionListener);
    buttonGroup.add(oneMenuItem);
    menu.add(oneMenuItem);

    JRadioButtonMenuItem twoMenuItem = new JRadioButtonMenuItem("Turtle Doves", true);
    twoMenuItem.addActionListener(actionListener);
    buttonGroup.add(twoMenuItem);
    menu.add(twoMenuItem);

    JRadioButtonMenuItem threeMenuItem = new JRadioButtonMenuItem("French Hens", threeIcon);
    threeMenuItem.addItemListener(itemListener);
    buttonGroup.add(threeMenuItem);
    menu.add(threeMenuItem);

    JRadioButtonMenuItem fourMenuItem = new JRadioButtonMenuItem("Calling Birds", fourIcon, true);
    fourMenuItem.addActionListener(actionListener);
    buttonGroup.add(fourMenuItem);
    menu.add(fourMenuItem);

    JRadioButtonMenuItem fiveMenuItem = new JRadioButtonMenuItem(fiveIcon);
    fiveMenuItem.addActionListener(actionListener);
    fiveMenuItem.setActionCommand("Rings");
    buttonGroup.add(fiveMenuItem);
    menu.add(fiveMenuItem);

    JRadioButtonMenuItem sixMenuItem = new JRadioButtonMenuItem(sixIcon, true);
    sixMenuItem.addActionListener(actionListener);
    sixMenuItem.setActionCommand("Geese");
    buttonGroup.add(sixMenuItem);
    menu.add(sixMenuItem);

    menuBar.add(menu);
    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    frame.setVisible(true);
}