How to set Mnemonic key for selection of each JCheckBox in Java?

Java 8Object Oriented ProgrammingProgramming

Mnemonic key is set so that a user can use Keyboard keys to check a CheckBox. For example, a key can be set with ALT:

checkBox1.setMnemonic(KeyEvent.VK_F);
checkBox2.setMnemonic(KeyEvent.VK_T);
checkBox3.setMnemonic(KeyEvent.VK_R);
checkBox4.setMnemonic(KeyEvent.VK_C);
checkBox5.setMnemonic(KeyEvent.VK_A);

Above, we have set key ALT+F for checkbox 1, key ALT+T for checkBox2, etc.

The following is an example. Here, we have set Mnemonic key for selection of each CheckBox:

Example

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;
   public SwingDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
      SwingDemo swingControlDemo = new SwingDemo();
      swingControlDemo.showCheckBoxDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing");
      mainFrame.setSize(600,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }
      });
      headerLabel = new JLabel("", JLabel.CENTER);
      statusLabel = new JLabel("",JLabel.CENTER);
      statusLabel.setSize(350,100);
      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());
      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);
   }
   private void showCheckBoxDemo(){
      headerLabel.setText("Favourite Sports");
      final JCheckBox checkBox1 = new JCheckBox("Football");
      final JCheckBox checkBox2 = new JCheckBox("Tennis");
      final JCheckBox checkBox3 = new JCheckBox("Rugby");
      final JCheckBox checkBox4 = new JCheckBox("Cricket");
      final JCheckBox checkBox5 = new JCheckBox("Racing");
      checkBox1.setMnemonic(KeyEvent.VK_F);
      checkBox2.setMnemonic(KeyEvent.VK_T);
      checkBox3.setMnemonic(KeyEvent.VK_R);
      checkBox4.setMnemonic(KeyEvent.VK_C);
      checkBox5.setMnemonic(KeyEvent.VK_A);
      checkBox1.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {
            statusLabel.setText("Football Checkbox: "+ (e.getStateChange()==1?"checked":"unchecked"));
         }
      });
      checkBox2.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {
            statusLabel.setText("Tennis Checkbox: "+ (e.getStateChange()==1?"checked":"unchecked"));
         }
      });
      checkBox3.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {
            statusLabel.setText("Rugby Checkbox is "+ (e.getStateChange()==1?"checked":"unchecked"));
         }
      });
      checkBox4.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {
            statusLabel.setText("Cricket Checkbox is "+ (e.getStateChange()==1?"checked":"unchecked"));
         }
      });
      checkBox5.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {
            statusLabel.setText("Racing Checkbox is "+ (e.getStateChange()==1?"checked":"unchecked"));
         }
      });
      controlPanel.add(checkBox1);
      controlPanel.add(checkBox2);
      controlPanel.add(checkBox3);
      controlPanel.add(checkBox4);
      controlPanel.add(checkBox5);
      mainFrame.setVisible(true);
   }
}

Output

Now, Press and the key which is set for checkbox. Let’s say first for Football, we will press Alt+F, since the same we have set under the code (setMnemonic(KeyEvent.VK_F)):

Above, now the Football checkbox is checked since we used the key Alt+F

raja
Published on 06-May-2019 11:31:15
Advertisements