Example usage for javax.swing JMenu JMenu

List of usage examples for javax.swing JMenu JMenu

Introduction

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

Prototype

public JMenu(Action a) 

Source Link

Document

Constructs a menu whose properties are taken from the Action supplied.

Usage

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);/* w ww.j a va 2 s .  co  m*/

    MenuListener lis = new MenuListener() {

        public void menuSelected(MenuEvent e) {
            System.out.println("menuSelected");
        }

        public void menuDeselected(MenuEvent e) {
            System.out.println("menuDeselected");

        }

        public void menuCanceled(MenuEvent e) {
            System.out.println("menuCanceled");

        }
    };

    fileMenu.addMenuListener(lis);

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

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

    fileMenu.removeMenuListener(lis);
}

From source file:ContructMenuWithAction.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   www .j  a v a 2 s.c o  m

    fileMenu.addMenuListener(new MenuListener() {

        public void menuSelected(MenuEvent e) {
            System.out.println("menuSelected");
        }

        public void menuDeselected(MenuEvent e) {
            System.out.println("menuDeselected");

        }

        public void menuCanceled(MenuEvent e) {
            System.out.println("menuCanceled");

        }
    });

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

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

From source file:ContructMenuMenuKeyListener.java

License:asdf

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  ava  2s.  co  m

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

    newMenuItem.addMenuKeyListener(new MenuKeyListener() {

        public void menuKeyTyped(MenuKeyEvent e) {
            System.out.println("KeyTyped");
        }

        public void menuKeyPressed(MenuKeyEvent e) {
            System.out.println("KeyPressed");
        }

        public void menuKeyReleased(MenuKeyEvent e) {
            System.out.println("KeyReleased");
        }
    });

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

From source file:CheckMenuItem.java

public static void main(String[] args) {
    final JLabel statusbar = new JLabel(" Statusbar");

    JMenuBar menubar = new JMenuBar();
    JMenu file = new JMenu("File");
    file.setMnemonic(KeyEvent.VK_F);

    JMenu view = new JMenu("View");
    view.setMnemonic(KeyEvent.VK_V);

    JCheckBoxMenuItem sbar = new JCheckBoxMenuItem("Show StatuBar");
    sbar.setState(true);/*from w  w w .java 2s  .c  o  m*/

    sbar.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            if (statusbar.isVisible()) {
                statusbar.setVisible(false);
            } else {
                statusbar.setVisible(true);
            }
        }
    });

    view.add(sbar);

    menubar.add(file);
    menubar.add(view);

    JFrame f = new JFrame();
    f.setJMenuBar(menubar);
    statusbar.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
    f.add(statusbar, BorderLayout.SOUTH);
    f.setSize(360, 250);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:ContructMenuMenuDragMouseListener.java

License:asdf

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  .ja va2s .  c om*/

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

    newMenuItem.addMenuDragMouseListener(new MenuDragMouseListener() {

        public void menuDragMouseEntered(MenuDragMouseEvent e) {
            System.out.println("menuDragMouseEntered");
        }

        public void menuDragMouseExited(MenuDragMouseEvent e) {
            System.out.println("menuDragMouseExited");

        }

        public void menuDragMouseDragged(MenuDragMouseEvent e) {
            System.out.println("menuDragMouseDragged");

        }

        public void menuDragMouseReleased(MenuDragMouseEvent e) {
            System.out.println("menuDragMouseReleased");

        }
    });

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

From source file:RadioButtonMenuSample.java

public static void main(String args[]) {
    JFrame f = new JFrame("JRadioButtonMenuItem Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar bar = new JMenuBar();
    JMenu menu = new JMenu("Options");
    menu.setMnemonic(KeyEvent.VK_O);

    ButtonGroup group = new ButtonGroup();

    JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem("North");
    group.add(menuItem);//from   w w  w.java  2 s  .  c om
    menu.add(menuItem);

    menuItem = new JRadioButtonMenuItem("East");
    group.add(menuItem);
    menu.add(menuItem);

    menuItem = new JRadioButtonMenuItem("West");
    group.add(menuItem);
    menu.add(menuItem);

    menuItem = new JRadioButtonMenuItem("South");
    group.add(menuItem);
    menu.add(menuItem);

    menuItem = new JRadioButtonMenuItem("Center");
    group.add(menuItem);
    menu.add(menuItem);

    bar.add(menu);
    f.setJMenuBar(bar);
    f.setSize(300, 200);
    f.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);//w w w .j  a  va  2s  . c o m

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

    JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem(new MyIcon(Color.RED));
    caseMenuItem.setMnemonic(KeyEvent.VK_C);
    fileMenu.add(caseMenuItem);

    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    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 . ja  va2s .c o m

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

    JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Item", new MyIcon(Color.RED), true);
    caseMenuItem.setMnemonic(KeyEvent.VK_C);
    fileMenu.add(caseMenuItem);

    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    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);//w  w  w .jav a  2 s  .c  o  m

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

    JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Item", new MyIcon(Color.RED));
    caseMenuItem.setMnemonic(KeyEvent.VK_C);
    fileMenu.add(caseMenuItem);

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

From source file:Submenu.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  ww w  .  jav  a 2s .  c  o  m

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

    // Edit->Options Submenu, O - Mnemonic, at.gif - Icon Image File
    JMenu findOptionsMenu = new JMenu("Options");
    Icon atIcon = new ImageIcon("at.gif");
    findOptionsMenu.setIcon(atIcon);
    findOptionsMenu.setMnemonic(KeyEvent.VK_O);
    fileMenu.add(findOptionsMenu);

    // ButtonGroup for radio buttons
    ButtonGroup directionGroup = new ButtonGroup();

    // Edit->Options->Forward, F - Mnemonic, in group
    JRadioButtonMenuItem forwardMenuItem = new JRadioButtonMenuItem("Forward", true);
    forwardMenuItem.setMnemonic(KeyEvent.VK_F);
    findOptionsMenu.add(forwardMenuItem);
    directionGroup.add(forwardMenuItem);

    // Edit->Options->Backward, B - Mnemonic, in group
    JRadioButtonMenuItem backwardMenuItem = new JRadioButtonMenuItem("Backward");
    backwardMenuItem.setMnemonic(KeyEvent.VK_B);
    findOptionsMenu.add(backwardMenuItem);
    directionGroup.add(backwardMenuItem);

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