Java JRadioButtonMenuItem set mnemonic key

Description

Java JRadioButtonMenuItem set mnemonic key


import java.awt.FlowLayout;
import java.awt.event.KeyEvent;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingUtilities;

public class Main {
  public Main() {

    JFrame jfrm = new JFrame("Menu Demo");
    jfrm.setLayout(new FlowLayout());
    jfrm.setSize(220, 200);/*from   www .  j ava  2  s.  co m*/
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar jmb = new JMenuBar();

    // Create the File menu.
    JMenu jmFile = new JMenu("File");
    JRadioButtonMenuItem m1 = new JRadioButtonMenuItem("A");
    m1.setSelected(true);
    m1.setMnemonic(KeyEvent.VK_A);

    JRadioButtonMenuItem m2 = new JRadioButtonMenuItem("B");
    m2.setMnemonic(KeyEvent.VK_B);
    
    ButtonGroup bg = new ButtonGroup();
    bg.add(m1);
    bg.add(m2);
    

    jmFile.add(m1);
    jmFile.add(m2);
    
    
    jmb.add(jmFile);

    jfrm.setJMenuBar(jmb);

    jfrm.setVisible(true);
  }

  public static void main(String args[]) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        new Main();
      }
    });
  }
}



PreviousNext

Related