Java JCheckBoxMenuItem set mnemonic key

Description

Java JCheckBoxMenuItem set mnemonic key

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

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.SwingUtilities;

public class Main {
  public Main() {

    JFrame jfrm = new JFrame("Menu Demo");
    jfrm.setLayout(new FlowLayout());
    jfrm.setSize(220, 200);//from w ww . java 2 s.c o  m
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar jmb = new JMenuBar();

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

    JCheckBoxMenuItem m2 = new JCheckBoxMenuItem("B");
    m2.setSelected(true);
    m2.setMnemonic(KeyEvent.VK_B);

    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