Handle JCheckBox Events with an ItemListener in Java

Java 8Object Oriented ProgrammingProgramming

Here, we have used ItemListener to handle JCheckBox events i.e. whenever any of the CheckBox is selected.

For example; When any of the sports like Football CheckBox is checked, event is fired and a message is visible in the botton.

The following is an example to handle JCheckBox events with an ItemListener:

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(400,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);
   }
}

The output is as follows displaying the frame:

Now, select any of the checkbox and the status would be visible in the bottom

Now, uncheck any of the checkbox and the status would be visible in the bottom:

raja
Published on 06-May-2019 11:16:26
Advertisements