Tuesday, January 7, 2014

Add ActionListener to JMenuItem, JCheckBoxMenuItem and JRadioButtonMenuItem

This example add ActionListener to JMenuItem, JCheckBoxMenuItem and JRadioButtonMenuItem:
Add ActionListener to JMenuItem, JCheckBoxMenuItem and JRadioButtonMenuItem
Add ActionListener to JMenuItem, JCheckBoxMenuItem and JRadioButtonMenuItem

package javamenu;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
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 {
    
    JLabel jLabel;

    private void prepareUI() {
        
        jLabel = new JLabel("Label", JLabel.RIGHT);
        getContentPane().add(jLabel);
        
        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);
       
        //Add ActionListener
        menuItemNew.addActionListener(menuActionListener);
        menuItemOpen.addActionListener(menuActionListener);
        menuItemSave.addActionListener(menuActionListener);
        jCheckBoxMenuItem_A.addActionListener(menuActionListener);
        jCheckBoxMenuItem_B.addActionListener(menuActionListener);
        jCheckBoxMenuItem_C.addActionListener(menuActionListener);
        jRadioButtonMenuItem_D.addActionListener(menuActionListener);
        jRadioButtonMenuItem_E.addActionListener(menuActionListener);
        jRadioButtonMenuItem_F.addActionListener(menuActionListener);
    }

    ActionListener menuActionListener = new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {
            jLabel.setText(e.getActionCommand());
        }
        
    };

    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();
        });
    }

}

No comments:

Post a Comment