Set Shortcut Key to a JCheckBox in Java



In this article, we will learn to set the shortcut key to a JCheckBox in Java. To create a JCheckBox with a keyboard shortcut (Alt + C), we will use Java Swing.

When we either click the checkbox with the mouse or press Alt+C, the checkbox toggles, and a message dialog is displayed.

We can do this by using the setMnemonic('C') method to assign the shortcut key and an ActionListener to respond to the checkbox selection.

What is a JCheckBox?

A JCheckBox is a subclass of JToggleButton, and it can be a small box that is either checked or unchecked. When we click on a JCheckBox, it changes from checked to unchecked or from unchecked to checked.


A JCheckBox can generate an ActionListener or ItemListener whenever the checkbox is changed.

What is a shortcut key?

A shortcut key is a combination of keys that performs an action without using the mouse. The following are some common keyboard shortcuts:

  • Ctrl+C: Used for copying the content.
  • Ctrl+V: Used for pasting the content.
  • Ctrl+A: Used to select the entire content.
  • Ctrl+Z: Used to undo or go to the previous content.

Adding a shortcut key to a JCheckBox in Java?

We can set the shortcut keys to a JCheckBox by using the setMnemonic() method.

setMnemonic() Method

The setMnemonic() is a method of the AbstractButton class and is used to set the mnemonic for a button. This method is only designed to handle character values which fall between 'a' and 'z' or 'A' and 'Z'. It takes only a single argument as a char for setting a key as the mnemonic.

Syntax

The following is the syntax for the setMnemonic() method declaration:

setMnemonic(char mnemonic)

AbstractButton class

Defines default behavior for menus and buttons. Buttons are configured and, for the most part, controlled by Actions. Placing an Action on a button has several benefits compared to configuring a button itself.

Setting the shortcut key to a JCheckBox

To set the shortcut key to a JCheckBox, first, we will create a class JCheckBoxShortCutKeyTest that extends the JFrame and declare a private JCheckBox named "checkBox". Then we will initialize the class constructor and set the title of the frame as "JCheckBoxShortCutKey Test".

Initialize the set the checkbox label to "Check or Press ALT-C", then add a light gray border around the checkbox by using the setBorder() method.

checkBox = new JCheckBox("Check or Press ALT-C");
checkBox.setBorder(BorderFactory.createLineBorder(Color.lightGray));

The setMnemonic('C') assigns Alt+C as the keyboard shortcut by pressing Alt+C will toggle the checkbox in the JFrame and add the frame in the center using the BorderLayout.

checkBox.setBorder(BorderFactory.createLineBorder(Color.lightGray));
checkBox.setMnemonic('C');
add(checkBox, BorderLayout.CENTER);

Triggers the ActionListener when the checkbox is clicked (mouse). The Alt+C shortcut is pressed (keyboard) and shows a dialog with the message "A Checkbox checked or pressed".

checkBox.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
      JOptionPane.showMessageDialog(null, "A Checkbox checked or pressed");
   }
});

Example

Below is an example of setting the shortcut key to a JCheckBox in Java:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JCheckBoxShortCutKeyTest extends JFrame {
   private JCheckBox checkBox;
   public JCheckBoxShortCutKeyTest() {
      setTitle("JCheckBoxShortCutKey Test");
      checkBox = new JCheckBox("Check or Press ALT-C");
      checkBox.setBorder(BorderFactory.createLineBorder(Color.lightGray));
      checkBox.setMnemonic('C');
      add(checkBox, BorderLayout.CENTER);
      checkBox.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent evt) {
            JOptionPane.showMessageDialog(null, "A Checkbox checked or pressed");
         }
      });
      setSize(400, 300);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setVisible(true);
   }
   public static void main(String args[]) throws Exception {
      new JCheckBoxShortCutKeyTest();
   }
}

Output

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-05-21T12:34:41+05:30

297 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements