Assigning a shortcut key to a JCheckBox

Swings JButton inherits from AbstractButton that defines a method setMnemonic.
You can use it as follows:

import javax.swing.*;
import java.util.*;
import java.awt.*;
   
public class Main extends JFrame
{
   public Main() throws Exception {
      JCheckBox cbox1 = new JCheckBox("Check me or press ALT-C");
      JCheckBox cbox2 = new JCheckBox("Check me or press ALT-D");
      cbox1.setMnemonic('C');
      cbox2.setMnemonic('D');
 
      getContentPane().setLayout(new GridLayout(2, 1));
      getContentPane().add(cbox1);
      getContentPane().add(cbox2);
      pack();
 
      cbox1.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent evt) {
            System.out.println("First JCheckBox checked!");
         }
      });

      cbox2.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent evt) {
            System.out.println("Second JCheckBox checked!");
         }
      });
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
   }
 
   public static void main(String args[]) throws Exception {
      Main main = new Main();
      main.show();
   }
}