Swing JCheckBox Class



package com.c4learn.swing;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class JCheckBoxDemo {

  private JFrame mainFrame;
  private JLabel headLabel;
  private JLabel msgLabel;
  private JPanel mainPanel;

  // Constructor
  public JCheckBoxDemo() {
    mainFrame = new JFrame("Java Swing Examples");
    mainFrame.setSize(300, 300);
    mainFrame.setLayout(new GridLayout(3, 1));

    headLabel = new JLabel("JCheckBox Demo", JLabel.CENTER);
    msgLabel = new JLabel("Empty Selection", JLabel.CENTER);
    mainPanel = new JPanel();
    mainPanel.setLayout(new FlowLayout());

    mainFrame.add(headLabel);
    mainFrame.add(mainPanel);
    mainFrame.add(msgLabel);
    mainFrame.setVisible(true);
  }

  public static void main(String[] args) {
    JCheckBoxDemo swingControlDemo = new JCheckBoxDemo();
    swingControlDemo.showButtonDemo();
  }

  private void showButtonDemo() {

    JCheckBox chkHindi = new JCheckBox("Hindi");
    JCheckBox chkEnglish = new JCheckBox("English");
    JCheckBox chkTamil = new JCheckBox("Tamil");

    chkHindi.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent e) {
        msgLabel.setText("Hindi : "
        + (e.getStateChange() == 1 ? "Select":"Deselect"));
      }
    });

    chkEnglish.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent e) {
        msgLabel.setText("English : "
        + (e.getStateChange() == 1 ? "Select":"Deselect"));
      }
    });
    
    chkTamil.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent e) {
        msgLabel.setText("Tamil : "
        + (e.getStateChange() == 1 ? "Select":"Deselect"));
      }
    });
    
    mainPanel.add(chkHindi);
    mainPanel.add(chkEnglish);
    mainPanel.add(chkTamil);

    mainFrame.setVisible(true);
  }
}

Output :

Swing Checkbox