Monday, January 6, 2014

Java JMenu Example

Example of using JMenuBar, JMenu, JMenuItem, JCheckBoxMenuItem, JRadioButtonMenuItem and ButtonGroup.
Example of Java JMenu
Example of Java JMenu

package javamenu;

import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingUtilities;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaMenu extends JFrame {

    private void prepareUI() {
        JMenuBar jMenuBar = new JMenuBar();

        JMenu menuFile = new JMenu("File");
        menuFile.setMnemonic(KeyEvent.VK_F);
        jMenuBar.add(menuFile);
        JMenu menuOption = new JMenu("Options");
        menuOption.setMnemonic(KeyEvent.VK_P);
        jMenuBar.add(menuOption);

        //MenuItem New, Open, Save under File
        JMenuItem menuItemNew = new JMenuItem("New", KeyEvent.VK_N);
        menuFile.add(menuItemNew);
        JMenuItem menuItemOpen = new JMenuItem("Open", KeyEvent.VK_O);
        menuFile.add(menuItemOpen);
        JMenuItem menuItemSave = new JMenuItem("Save", KeyEvent.VK_S);
        menuFile.add(menuItemSave);

        //CheckBox A, B, C under Options
        JCheckBoxMenuItem jCheckBoxMenuItem_A = new JCheckBoxMenuItem("Check A");
        jCheckBoxMenuItem_A.setMnemonic(KeyEvent.VK_A);
        menuOption.add(jCheckBoxMenuItem_A);
        
        JCheckBoxMenuItem jCheckBoxMenuItem_B = new JCheckBoxMenuItem("Check B");
        jCheckBoxMenuItem_B.setMnemonic(KeyEvent.VK_B);
        menuOption.add(jCheckBoxMenuItem_B);
        
        JCheckBoxMenuItem jCheckBoxMenuItem_C = new JCheckBoxMenuItem("Check C");
        jCheckBoxMenuItem_C.setMnemonic(KeyEvent.VK_C);
        menuOption.add(jCheckBoxMenuItem_C);
        menuOption.addSeparator();
        
        //Create ButtonGroup for radio button D, E, F
        ButtonGroup buttonGroup = new ButtonGroup();
        
        JRadioButtonMenuItem jRadioButtonMenuItem_D = new JRadioButtonMenuItem("Option D", true);
        jRadioButtonMenuItem_D.setMnemonic(KeyEvent.VK_D);
        menuOption.add(jRadioButtonMenuItem_D);
        buttonGroup.add(jRadioButtonMenuItem_D);

        JRadioButtonMenuItem jRadioButtonMenuItem_E = new JRadioButtonMenuItem("Option E");
        jRadioButtonMenuItem_E.setMnemonic(KeyEvent.VK_E);
        menuOption.add(jRadioButtonMenuItem_E);
        buttonGroup.add(jRadioButtonMenuItem_E);
        
        JRadioButtonMenuItem jRadioButtonMenuItem_F = new JRadioButtonMenuItem("Option F");
        jRadioButtonMenuItem_F.setMnemonic(KeyEvent.VK_F);
        menuOption.add(jRadioButtonMenuItem_F);
        buttonGroup.add(jRadioButtonMenuItem_F);
        
        setJMenuBar(jMenuBar);
    }

    private static void createAndShowGUI() {
        JavaMenu myFrame = new JavaMenu();
        myFrame.setTitle("java-buddy.blogspot.com");
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setSize(400, 300);
        myFrame.prepareUI();
        myFrame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            createAndShowGUI();
        });
    }

}



Next:
- Add ActionListener to JMenuItem, JCheckBoxMenuItem and JRadioButtonMenuItem

No comments:

Post a Comment