Example usage for javax.swing JRadioButtonMenuItem JRadioButtonMenuItem

List of usage examples for javax.swing JRadioButtonMenuItem JRadioButtonMenuItem

Introduction

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

Prototype

public JRadioButtonMenuItem(Action a) 

Source Link

Document

Creates a radio button menu item whose properties are taken from the Action supplied.

Usage

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();//ww w  . j  a va 2 s.c om

    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:components.MenuLookDemo.java

public JMenuBar createMenuBar() {
    JMenuBar menuBar;//from  w  ww.  j  av  a2  s.c  om
    JMenu menu, submenu;
    JMenuItem menuItem;
    JRadioButtonMenuItem rbMenuItem;
    JCheckBoxMenuItem cbMenuItem;

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

    //Build the first menu.
    menu = new JMenu("A Menu");
    menu.setMnemonic(KeyEvent.VK_A);
    menu.getAccessibleContext().setAccessibleDescription("The only menu in this program that has menu items");
    menuBar.add(menu);

    //a group of JMenuItems
    menuItem = new JMenuItem("A text-only menu item", KeyEvent.VK_T);
    //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
    menuItem.getAccessibleContext().setAccessibleDescription("This doesn't really do anything");
    menu.add(menuItem);

    ImageIcon icon = createImageIcon("images/middle.gif");
    menuItem = new JMenuItem("Both text and icon", icon);
    menuItem.setMnemonic(KeyEvent.VK_B);
    menu.add(menuItem);

    menuItem = new JMenuItem(icon);
    menuItem.setMnemonic(KeyEvent.VK_D);
    menu.add(menuItem);

    //a group of radio button menu items
    menu.addSeparator();
    ButtonGroup group = new ButtonGroup();

    rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
    rbMenuItem.setSelected(true);
    rbMenuItem.setMnemonic(KeyEvent.VK_R);
    group.add(rbMenuItem);
    menu.add(rbMenuItem);

    rbMenuItem = new JRadioButtonMenuItem("Another one");
    rbMenuItem.setMnemonic(KeyEvent.VK_O);
    group.add(rbMenuItem);
    menu.add(rbMenuItem);

    //a group of check box menu items
    menu.addSeparator();
    cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
    cbMenuItem.setMnemonic(KeyEvent.VK_C);
    menu.add(cbMenuItem);

    cbMenuItem = new JCheckBoxMenuItem("Another one");
    cbMenuItem.setMnemonic(KeyEvent.VK_H);
    menu.add(cbMenuItem);

    //a submenu
    menu.addSeparator();
    submenu = new JMenu("A submenu");
    submenu.setMnemonic(KeyEvent.VK_S);

    menuItem = new JMenuItem("An item in the submenu");
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK));
    submenu.add(menuItem);

    menuItem = new JMenuItem("Another item");
    submenu.add(menuItem);
    menu.add(submenu);

    //Build second menu in the menu bar.
    menu = new JMenu("Another Menu");
    menu.setMnemonic(KeyEvent.VK_N);
    menu.getAccessibleContext().setAccessibleDescription("This menu does nothing");
    menuBar.add(menu);

    return menuBar;
}

From source file:MenuLookDemo.java

public JMenuBar createMenuBar() {
    JMenuBar menuBar;//from   w  w  w  . ja v a  2  s.c o  m
    JMenu menu, submenu;
    JMenuItem menuItem;
    JRadioButtonMenuItem rbMenuItem;
    JCheckBoxMenuItem cbMenuItem;

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

    // Build the first menu.
    menu = new JMenu("A Menu");
    menu.setMnemonic(KeyEvent.VK_A);
    menu.getAccessibleContext().setAccessibleDescription("The only menu in this program that has menu items");
    menuBar.add(menu);

    // a group of JMenuItems
    menuItem = new JMenuItem("A text-only menu item", KeyEvent.VK_T);
    // menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
    menuItem.getAccessibleContext().setAccessibleDescription("This doesn't really do anything");
    menu.add(menuItem);

    ImageIcon icon = createImageIcon("images/middle.gif");
    menuItem = new JMenuItem("Both text and icon", icon);
    menuItem.setMnemonic(KeyEvent.VK_B);
    menu.add(menuItem);

    menuItem = new JMenuItem(icon);
    menuItem.setMnemonic(KeyEvent.VK_D);
    menu.add(menuItem);

    // a group of radio button menu items
    menu.addSeparator();
    ButtonGroup group = new ButtonGroup();

    rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
    rbMenuItem.setSelected(true);
    rbMenuItem.setMnemonic(KeyEvent.VK_R);
    group.add(rbMenuItem);
    menu.add(rbMenuItem);

    rbMenuItem = new JRadioButtonMenuItem("Another one");
    rbMenuItem.setMnemonic(KeyEvent.VK_O);
    group.add(rbMenuItem);
    menu.add(rbMenuItem);

    // a group of check box menu items
    menu.addSeparator();
    cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
    cbMenuItem.setMnemonic(KeyEvent.VK_C);
    menu.add(cbMenuItem);

    cbMenuItem = new JCheckBoxMenuItem("Another one");
    cbMenuItem.setMnemonic(KeyEvent.VK_H);
    menu.add(cbMenuItem);

    // a submenu
    menu.addSeparator();
    submenu = new JMenu("A submenu");
    submenu.setMnemonic(KeyEvent.VK_S);

    menuItem = new JMenuItem("An item in the submenu");
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK));
    submenu.add(menuItem);

    menuItem = new JMenuItem("Another item");
    submenu.add(menuItem);
    menu.add(submenu);

    // Build second menu in the menu bar.
    menu = new JMenu("Another Menu");
    menu.setMnemonic(KeyEvent.VK_N);
    menu.getAccessibleContext().setAccessibleDescription("This menu does nothing");
    menuBar.add(menu);

    return menuBar;
}

From source file:ToolbarDemo.java

protected JMenuBar createMenuBar() {
    final JMenuBar menuBar = new JMenuBar();

    JMenu mFile = new JMenu("File");
    mFile.setMnemonic('f');

    ImageIcon iconNew = new ImageIcon("file_new.gif");
    Action actionNew = new AbstractAction("New", iconNew) {
        public void actionPerformed(ActionEvent e) {
            System.out.println("new action");
        }/*from   ww  w . j  a  va 2s .co m*/
    };
    JMenuItem item = mFile.add(actionNew);
    mFile.add(item);

    ImageIcon iconOpen = new ImageIcon("file_open.gif");
    Action actionOpen = new AbstractAction("Open...", iconOpen) {
        public void actionPerformed(ActionEvent e) {
            System.out.println("open action");
        }
    };
    item = mFile.add(actionOpen);
    mFile.add(item);

    ImageIcon iconSave = new ImageIcon("file_save.gif");
    Action actionSave = new AbstractAction("Save...", iconSave) {
        public void actionPerformed(ActionEvent e) {
            System.out.println("save action");
        }
    };
    item = mFile.add(actionSave);
    mFile.add(item);

    mFile.addSeparator();

    Action actionExit = new AbstractAction("Exit") {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    };
    item = mFile.add(actionExit);
    item.setMnemonic('x');
    menuBar.add(mFile);

    toolBar = new JToolBar();
    JButton btn1 = toolBar.add(actionNew);
    btn1.setToolTipText("New text");
    JButton btn2 = toolBar.add(actionOpen);
    btn2.setToolTipText("Open text file");
    JButton btn3 = toolBar.add(actionSave);
    btn3.setToolTipText("Save text file");

    ActionListener fontListener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            updateMonitor();
        }
    };

    JMenu mFont = new JMenu("Font");
    mFont.setMnemonic('o');

    ButtonGroup group = new ButtonGroup();
    fontMenus = new JMenuItem[FontNames.length];
    for (int k = 0; k < FontNames.length; k++) {
        int m = k + 1;
        fontMenus[k] = new JRadioButtonMenuItem(m + " " + FontNames[k]);
        boolean selected = (k == 0);
        fontMenus[k].setSelected(selected);
        fontMenus[k].setMnemonic('1' + k);
        fontMenus[k].setFont(fonts[k]);
        fontMenus[k].addActionListener(fontListener);
        group.add(fontMenus[k]);
        mFont.add(fontMenus[k]);
    }

    mFont.addSeparator();

    boldMenu.setMnemonic('b');
    Font fn = fonts[1].deriveFont(Font.BOLD);
    boldMenu.setFont(fn);
    boldMenu.setSelected(false);
    boldMenu.addActionListener(fontListener);
    mFont.add(boldMenu);

    italicMenu.setMnemonic('i');
    fn = fonts[1].deriveFont(Font.ITALIC);
    italicMenu.setFont(fn);
    italicMenu.setSelected(false);
    italicMenu.addActionListener(fontListener);
    mFont.add(italicMenu);

    menuBar.add(mFont);

    getContentPane().add(toolBar, BorderLayout.NORTH);

    return menuBar;
}

From source file:MenuDemo.java

protected JMenuBar createMenuBar() {
    final JMenuBar menuBar = new JMenuBar();

    JMenu menuFile = new JMenu("File");
    menuFile.setMnemonic('f');

    JMenuItem menuItem = new JMenuItem("New");
    menuItem.setIcon(new ImageIcon("file_new.gif"));
    menuItem.setMnemonic('n');
    ActionListener lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("New");
        }/* w w w .j a v a  2  s  .  c om*/
    };
    menuItem.addActionListener(lst);
    menuFile.add(menuItem);

    menuItem = new JMenuItem("Open...");
    menuItem.setIcon(new ImageIcon("file_open.gif"));
    menuItem.setMnemonic('o');
    lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            MenuDemo.this.repaint();
            if (fileChooser.showOpenDialog(MenuDemo.this) != JFileChooser.APPROVE_OPTION)
                return;
            System.out.println(fileChooser.getSelectedFile());
        }
    };
    menuItem.addActionListener(lst);
    menuFile.add(menuItem);

    menuItem = new JMenuItem("Save...");
    menuItem.setIcon(new ImageIcon("file_save.gif"));
    menuItem.setMnemonic('s');
    lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Save...");
        }
    };
    menuItem.addActionListener(lst);
    menuFile.add(menuItem);

    menuFile.addSeparator();

    menuItem = new JMenuItem("Exit");
    menuItem.setMnemonic('x');
    lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    };
    menuItem.addActionListener(lst);
    menuFile.add(menuItem);
    menuBar.add(menuFile);

    ActionListener fontListener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            updateMonitor();
        }
    };

    JMenu mFont = new JMenu("Font");
    mFont.setMnemonic('o');

    ButtonGroup group = new ButtonGroup();
    menus = new JMenuItem[FontNames.length];
    for (int i = 0; i < FontNames.length; i++) {
        int m = i + 1;
        menus[i] = new JRadioButtonMenuItem(m + " " + FontNames[i]);
        boolean selected = (i == 0);
        menus[i].setSelected(selected);
        menus[i].setMnemonic('1' + i);
        menus[i].setFont(fontArray[i]);
        menus[i].addActionListener(fontListener);
        group.add(menus[i]);
        mFont.add(menus[i]);
    }

    mFont.addSeparator();

    boldMenuItem = new JCheckBoxMenuItem("Bold");
    boldMenuItem.setMnemonic('b');
    Font fn = fontArray[1].deriveFont(Font.BOLD);
    boldMenuItem.setFont(fn);
    boldMenuItem.setSelected(false);
    boldMenuItem.addActionListener(fontListener);
    mFont.add(boldMenuItem);

    italicMenuItem = new JCheckBoxMenuItem("Italic");
    italicMenuItem.setMnemonic('i');
    fn = fontArray[1].deriveFont(Font.ITALIC);
    italicMenuItem.setFont(fn);
    italicMenuItem.setSelected(false);
    italicMenuItem.addActionListener(fontListener);
    mFont.add(italicMenuItem);

    menuBar.add(mFont);

    return menuBar;
}

From source file:ShowComponent.java

/**
 * This static method queries the system to find out what Pluggable
 * Look-and-Feel (PLAF) implementations are available. Then it creates a
 * JMenu component that lists each of the implementations by name and allows
 * the user to select one of them using JRadioButtonMenuItem components.
 * When the user selects one, the selected menu item traverses the component
 * hierarchy and tells all components to use the new PLAF.
 *//*from   w  w w .  j  a  v a 2s . c o  m*/
public static JMenu createPlafMenu(final JFrame frame) {
    // Create the menu
    JMenu plafmenu = new JMenu("Look and Feel");

    // Create an object used for radio button mutual exclusion
    ButtonGroup radiogroup = new ButtonGroup();

    // Look up the available look and feels
    UIManager.LookAndFeelInfo[] plafs = UIManager.getInstalledLookAndFeels();

    // Loop through the plafs, and add a menu item for each one
    for (int i = 0; i < plafs.length; i++) {
        String plafName = plafs[i].getName();
        final String plafClassName = plafs[i].getClassName();

        // Create the menu item
        JMenuItem item = plafmenu.add(new JRadioButtonMenuItem(plafName));

        // Tell the menu item what to do when it is selected
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    // Set the new look and feel
                    UIManager.setLookAndFeel(plafClassName);
                    // Tell each component to change its look-and-feel
                    SwingUtilities.updateComponentTreeUI(frame);
                    // Tell the frame to resize itself to the its
                    // children's new desired sizes
                    frame.pack();
                } catch (Exception ex) {
                    System.err.println(ex);
                }
            }

        });

        // Only allow one menu item to be selected at once
        radiogroup.add(item);
    }
    return plafmenu;
}

From source file:components.MenuDemo.java

public JMenuBar createMenuBar() {
    JMenuBar menuBar;/* w w w.  java  2s  .c o  m*/
    JMenu menu, submenu;
    JMenuItem menuItem;
    JRadioButtonMenuItem rbMenuItem;
    JCheckBoxMenuItem cbMenuItem;

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

    //Build the first menu.
    menu = new JMenu("A Menu");
    menu.setMnemonic(KeyEvent.VK_A);
    menu.getAccessibleContext().setAccessibleDescription("The only menu in this program that has menu items");
    menuBar.add(menu);

    //a group of JMenuItems
    menuItem = new JMenuItem("A text-only menu item", KeyEvent.VK_T);
    //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
    menuItem.getAccessibleContext().setAccessibleDescription("This doesn't really do anything");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    ImageIcon icon = createImageIcon("images/middle.gif");
    menuItem = new JMenuItem("Both text and icon", icon);
    menuItem.setMnemonic(KeyEvent.VK_B);
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem(icon);
    menuItem.setMnemonic(KeyEvent.VK_D);
    menuItem.addActionListener(this);
    menu.add(menuItem);

    //a group of radio button menu items
    menu.addSeparator();
    ButtonGroup group = new ButtonGroup();

    rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
    rbMenuItem.setSelected(true);
    rbMenuItem.setMnemonic(KeyEvent.VK_R);
    group.add(rbMenuItem);
    rbMenuItem.addActionListener(this);
    menu.add(rbMenuItem);

    rbMenuItem = new JRadioButtonMenuItem("Another one");
    rbMenuItem.setMnemonic(KeyEvent.VK_O);
    group.add(rbMenuItem);
    rbMenuItem.addActionListener(this);
    menu.add(rbMenuItem);

    //a group of check box menu items
    menu.addSeparator();
    cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
    cbMenuItem.setMnemonic(KeyEvent.VK_C);
    cbMenuItem.addItemListener(this);
    menu.add(cbMenuItem);

    cbMenuItem = new JCheckBoxMenuItem("Another one");
    cbMenuItem.setMnemonic(KeyEvent.VK_H);
    cbMenuItem.addItemListener(this);
    menu.add(cbMenuItem);

    //a submenu
    menu.addSeparator();
    submenu = new JMenu("A submenu");
    submenu.setMnemonic(KeyEvent.VK_S);

    menuItem = new JMenuItem("An item in the submenu");
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK));
    menuItem.addActionListener(this);
    submenu.add(menuItem);

    menuItem = new JMenuItem("Another item");
    menuItem.addActionListener(this);
    submenu.add(menuItem);
    menu.add(submenu);

    //Build second menu in the menu bar.
    menu = new JMenu("Another Menu");
    menu.setMnemonic(KeyEvent.VK_N);
    menu.getAccessibleContext().setAccessibleDescription("This menu does nothing");
    menuBar.add(menu);

    return menuBar;
}

From source file:org.broad.igv.peaks.PeakTrackMenu.java

public void addDisplayModeItems() {
    addSeparator();//from w  w w  .j  a  va2s .c  om
    ButtonGroup group = new ButtonGroup();

    Track.DisplayMode displayMode = track.getDisplayMode();

    JRadioButtonMenuItem m1 = new JRadioButtonMenuItem("Compressed");
    m1.setSelected(displayMode == Track.DisplayMode.COLLAPSED);
    m1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            track.setDisplayMode(Track.DisplayMode.COLLAPSED);
            IGV.getInstance().doRefresh();
        }
    });

    JRadioButtonMenuItem m3 = new JRadioButtonMenuItem("Time Series");
    m3.setSelected(displayMode == Track.DisplayMode.EXPANDED);
    m3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            track.setDisplayMode(Track.DisplayMode.EXPANDED);
            IGV.getInstance().doRefresh();
        }
    });

    add(m1);
    add(m3);
    group.add(m1);
    group.add(m3);

}

From source file:components.MenuSelectionManagerDemo.java

public JMenuBar createMenuBar() {
    JMenuBar menuBar;//from   www .  java  2s. c  o m
    JMenu menu, submenu;
    JMenuItem menuItem;
    JRadioButtonMenuItem rbMenuItem;
    JCheckBoxMenuItem cbMenuItem;

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

    //Build the first menu.
    menu = new JMenu("A Menu");
    menu.setMnemonic(KeyEvent.VK_A);
    menu.getAccessibleContext().setAccessibleDescription("The only menu in this program that has menu items");
    menuBar.add(menu);

    //a group of JMenuItems
    menuItem = new JMenuItem("A text-only menu item", KeyEvent.VK_T);
    //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
    menuItem.getAccessibleContext().setAccessibleDescription("This doesn't really do anything");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    ImageIcon icon = createImageIcon("images/middle.gif");
    menuItem = new JMenuItem("Both text and icon", icon);
    menuItem.setMnemonic(KeyEvent.VK_B);
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem(icon);
    menuItem.setMnemonic(KeyEvent.VK_D);
    menuItem.addActionListener(this);
    menu.add(menuItem);

    //a group of radio button menu items
    menu.addSeparator();
    ButtonGroup group = new ButtonGroup();

    rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
    rbMenuItem.setSelected(true);
    rbMenuItem.setMnemonic(KeyEvent.VK_R);
    group.add(rbMenuItem);
    rbMenuItem.addActionListener(this);
    menu.add(rbMenuItem);

    rbMenuItem = new JRadioButtonMenuItem("Another one");
    rbMenuItem.setMnemonic(KeyEvent.VK_O);
    group.add(rbMenuItem);
    rbMenuItem.addActionListener(this);
    menu.add(rbMenuItem);

    //a group of check box menu items
    menu.addSeparator();
    cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
    cbMenuItem.setMnemonic(KeyEvent.VK_C);
    cbMenuItem.addItemListener(this);
    menu.add(cbMenuItem);

    cbMenuItem = new JCheckBoxMenuItem("Another one");
    cbMenuItem.setMnemonic(KeyEvent.VK_H);
    cbMenuItem.addItemListener(this);
    menu.add(cbMenuItem);

    //a submenu
    menu.addSeparator();
    submenu = new JMenu("A submenu");
    submenu.setMnemonic(KeyEvent.VK_S);

    menuItem = new JMenuItem("An item in the submenu");
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK));
    menuItem.addActionListener(this);
    submenu.add(menuItem);

    menuItem = new JMenuItem("Another item");
    menuItem.addActionListener(this);
    submenu.add(menuItem);
    menu.add(submenu);

    //Build second menu in the menu bar.
    menu = new JMenu("Another Menu");
    menu.setMnemonic(KeyEvent.VK_N);
    menu.getAccessibleContext().setAccessibleDescription("This menu does nothing");
    menuBar.add(menu);

    Timer timer = new Timer(ONE_SECOND, new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            MenuElement[] path = MenuSelectionManager.defaultManager().getSelectedPath();
            for (int i = 0; i < path.length; i++) {
                if (path[i].getComponent() instanceof javax.swing.JMenuItem) {
                    JMenuItem mi = (JMenuItem) path[i].getComponent();
                    if ("".equals(mi.getText())) {
                        output.append("ICON-ONLY MENU ITEM > ");
                    } else {
                        output.append(mi.getText() + " > ");
                    }
                }
            }
            if (path.length > 0)
                output.append(newline);
        }
    });
    timer.start();
    return menuBar;
}

From source file:uk.co.petertribble.jkstat.gui.KstatBaseChartFrame.java

/**
 * Create the Sleep for... Menu./* www. j a  v a 2 s .  co  m*/
 *
 * @return the Sleep Menu
 */
protected JMenu sleepMenu() {
    JMenu jms = new JMenu(KstatResources.getString("SLEEP.TEXT"));
    jms.setMnemonic(KeyEvent.VK_U);
    sleepItem1 = new JRadioButtonMenuItem(KstatResources.getString("SLEEP.1"));
    sleepItem1.addActionListener(this);
    sleepItem2 = new JRadioButtonMenuItem(KstatResources.getString("SLEEP.2"));
    sleepItem2.addActionListener(this);
    sleepItem5 = new JRadioButtonMenuItem(KstatResources.getString("SLEEP.5"), true);
    sleepItem5.addActionListener(this);
    sleepItem10 = new JRadioButtonMenuItem(KstatResources.getString("SLEEP.10"));
    sleepItem10.addActionListener(this);
    jms.add(sleepItem1);
    jms.add(sleepItem2);
    jms.add(sleepItem5);
    jms.add(sleepItem10);

    ButtonGroup sleepGroup = new ButtonGroup();
    sleepGroup.add(sleepItem1);
    sleepGroup.add(sleepItem2);
    sleepGroup.add(sleepItem5);
    sleepGroup.add(sleepItem10);

    return jms;
}