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[] argv) throws Exception {
    JMenu menu = new JMenu("Menu Label");
    menu.add(new JMenuItem("Item Label"));

    boolean lwPopup = menu.getPopupMenu().isLightWeightPopupEnabled(); // true

    menu.getPopupMenu().setLightWeightPopupEnabled(false);
}

From source file:Main.java

License:asdf

public static void main(String[] argv) throws Exception {
    final JPopupMenu popupMenu = new JPopupMenu();

    JMenu submenu = new JMenu("SubMenu1");
    submenu.add("asdf");
    submenu.add("asdf");

    // Add submenu to popup menu
    popupMenu.add(submenu);//from  ww  w . j  a v  a2  s .  co m
}

From source file:RootExample2.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JRootPane root = f.getRootPane();

    // Create a menu bar
    JMenuBar bar = new JMenuBar();
    JMenu menu = new JMenu("File");
    bar.add(menu);// w  w  w  .jav  a2  s  .  c o m
    menu.add("Open");
    menu.add("Close");
    root.setJMenuBar(bar);

    // Add a button to the content pane
    root.getContentPane().add(new JButton("Hello World"));

    // Display the UI
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Toolkit tk = Toolkit.getDefaultToolkit();
    final Main main = new Main();

    tk.addAWTEventListener(main, AWTEvent.WINDOW_EVENT_MASK);

    final JFrame frame = new JFrame("");
    frame.setName("your frame");
    JMenuBar mb = new JMenuBar();
    JMenu menu = new JMenu("File");
    menu.add(new AbstractAction("Quit") {
        public void actionPerformed(ActionEvent evt) {
            try {
                main.saveSettings();//from  w w  w  .  jav  a 2 s  . c  o m
                System.exit(0);
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
    });
    mb.add(menu);
    frame.setJMenuBar(mb);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JMenu menu = new JMenu("Menu Label");

    JMenuItem item1 = new JMenuItem("Item Label");
    menu.add(item1);

    // Add separator
    menu.add(new JSeparator());

    // Add another menu item
    JMenuItem item2 = new JMenuItem("Item Label");
    menu.add(item2);/*  ww  w.j a  v a2  s  .c  om*/
}

From source file:ColorAction.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setTitle("SeparateGUITest");
    frame.setSize(300, 200);/*from  ww  w .  j av a  2  s .c o  m*/
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    JPanel panel = new JPanel();

    Action blueAction = new ColorAction("Blue", new ImageIcon("blue-ball.gif"), Color.blue, panel);
    Action yellowAction = new ColorAction("Yellow", new ImageIcon("yellow-ball.gif"), Color.yellow, panel);
    Action redAction = new ColorAction("Red", new ImageIcon("red-ball.gif"), Color.red, panel);

    panel.add(new JButton(yellowAction));
    panel.add(new JButton(blueAction));
    panel.add(new JButton(redAction));

    panel.registerKeyboardAction(yellowAction, KeyStroke.getKeyStroke(KeyEvent.VK_Y, 0),
            JComponent.WHEN_IN_FOCUSED_WINDOW);
    panel.registerKeyboardAction(blueAction, KeyStroke.getKeyStroke(KeyEvent.VK_B, 0),
            JComponent.WHEN_IN_FOCUSED_WINDOW);
    panel.registerKeyboardAction(redAction, KeyStroke.getKeyStroke(KeyEvent.VK_R, 0),
            JComponent.WHEN_IN_FOCUSED_WINDOW);

    Container contentPane = frame.getContentPane();
    contentPane.add(panel);

    JMenu m = new JMenu("Color");
    m.add(yellowAction);
    m.add(blueAction);
    m.add(redAction);
    JMenuBar mbar = new JMenuBar();
    mbar.add(m);
    frame.setJMenuBar(mbar);

    frame.show();
}

From source file:Main.java

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

    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");

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

    frame.setJMenuBar(menuBar);//from w  w w  .jav  a 2 s .c  o m
    frame.setSize(350, 250);
    frame.setVisible(true);

    fileMenu.setSelected(true);
}

From source file:Main.java

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

    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");

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

    frame.setJMenuBar(menuBar);//from   w ww . j av  a 2s. co  m
    frame.setSize(350, 250);
    frame.setVisible(true);

    fileMenu.setPopupMenuVisible(true);
}

From source file:PrintHelloAction.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   w w  w .  j  av  a  2s.  co m*/
    menu.add(new JMenuItem(printAction));
    JToolBar toolbar = new JToolBar();
    toolbar.add(new JButton(printAction));
    JButton enableButton = new JButton("Enable");
    ActionListener enableActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            printAction.setEnabled(true);
        }
    };
    enableButton.addActionListener(enableActionListener);
    JButton disableButton = new JButton("Disable");
    ActionListener disableActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            printAction.setEnabled(false);
        }
    };
    disableButton.addActionListener(disableActionListener);
    JButton relabelButton = new JButton("Relabel");
    ActionListener relabelActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            printAction.putValue(Action.NAME, "Changed Action Value");
        }
    };
    relabelButton.addActionListener(relabelActionListener);
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(enableButton);
    buttonPanel.add(disableButton);
    buttonPanel.add(relabelButton);
    frame.setJMenuBar(menuBar);
    frame.add(toolbar, BorderLayout.SOUTH);
    frame.add(buttonPanel, BorderLayout.NORTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:MainClass.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   w ww . j  a  va 2s  .co m
    menu.add(new JMenuItem(printAction));

    JToolBar toolbar = new JToolBar();
    toolbar.add(new JButton(printAction));

    JButton enableButton = new JButton("Enable");
    ActionListener enableActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            printAction.setEnabled(true);
        }
    };
    enableButton.addActionListener(enableActionListener);

    JButton disableButton = new JButton("Disable");
    ActionListener disableActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            printAction.setEnabled(false);
        }
    };
    disableButton.addActionListener(disableActionListener);

    JButton relabelButton = new JButton("Relabel");
    ActionListener relabelActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            printAction.putValue(Action.NAME, "Hello, World");
        }
    };
    relabelButton.addActionListener(relabelActionListener);

    JPanel buttonPanel = new JPanel();
    buttonPanel.add(enableButton);
    buttonPanel.add(disableButton);
    buttonPanel.add(relabelButton);

    frame.setJMenuBar(menuBar);

    frame.add(toolbar, BorderLayout.SOUTH);
    frame.add(buttonPanel, BorderLayout.NORTH);
    frame.setSize(300, 200);
    frame.setVisible(true);

}