AWT CheckBox Class

Checkbox control is used to turn an option on(true) or off(false). There  is label for each checkbox representing what the checkbox does.The state of a checkbox can be changed by clicking on it.
Following is the declaration for java.awt.Checkbox class:
public class Checkbox
   extends Component
      implements ItemSelectable,Accessible
Checkbox() 
Creates a check box with an empty string for its label.
Checkbox(String label)  
Creates a check box with the specified label.
Checkbox(String label, boolean state)  
Creates a check box with the specified label and sets the specified state.
Checkbox(String label, boolean state, CheckboxGroup group)  
Constructs a Checkbox with the specified label, set to the specified state, and in the specified check box group.
Checkbox(String label, CheckboxGroup group, boolean state)  
Creates a check box with the specified label, in the specified check box group, and set to the specified state.
void addItemListener(ItemListener l)  
Adds the specified item listener to receive item events from this check box.
void addNotify()  
Creates the peer of the Checkbox.
AccessibleContext getAccessibleContext() 
Gets the AccessibleContext associated with this Checkbox.
CheckboxGroup getCheckboxGroup()  
Determines this check box's group.
ItemListener[] getItemListeners()  
Returns an array of all the item listeners registered on this checkbox.
String getLabel()  
Gets the label of this check box.
<T extends EventListener>T[] getListeners(Class<T> listenerType)  
Returns an array of all the objects currently registered as FooListeners upon this Checkbox.
Object[] getSelectedObjects()  

Returns an array (length 1) containing the checkbox label or null if the checkbox is not selected.
boolean getState()  
Determines whether this check box is in the on or off state.
protected String paramString()  
Returns a string representing the state of this Checkbox.
protected void processEvent(AWTEvent e)  
Processes events on this check box.
protected void processItemEvent(ItemEvent e)  
Processes item events occurring on this check box by dispatching them to any registered ItemListener objects.
void removeItemListener(ItemListener l)  
Removes the specified item listener so that the item listener no longer receives item events from this check box.
void setCheckboxGroup(CheckboxGroup g)  
Sets this check box's group to the specified check box group.
void setLabel(String label)  
Sets this check box's label to be the string argument.
void setState(boolean state)  
Sets the state of this check box to the specified state.
This class inherits methods from the following classes:
java.awt.Component
java.awt.Component
java.lang.Object
java.lang.Object
Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;

public class AwtControlDemo {

   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;

   public AwtControlDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtControlDemo  awtControlDemo = new AwtControlDemo();
      awtControlDemo.showCheckBoxDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);

      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showCheckBoxDemo(){

      headerLabel.setText("Control in action: CheckBox"); 

      Checkbox chkApple = new Checkbox("Apple");
      Checkbox chkMango = new Checkbox("Mango");
      Checkbox chkPeer = new Checkbox("Peer");


      chkApple.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {             
            statusLabel.setText("Apple Checkbox: " 
            + (e.getStateChange()==1?"checked":"unchecked"));
         }
      });

      chkMango.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {
            statusLabel.setText("Mango Checkbox: " 
            + (e.getStateChange()==1?"checked":"unchecked"));
         }
      });

      chkPeer.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent e) {
            statusLabel.setText("Peer Checkbox: " 
            + (e.getStateChange()==1?"checked":"unchecked"));
         }
      });

      controlPanel.add(chkApple);
      controlPanel.add(chkMango);
      controlPanel.add(chkPeer);       

      mainFrame.setVisible(true);  
   }
}
Compile the program using command prompt. Go to D:/ > AWT  and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AwtControlDemo
Verify the following output

    13 Lectures 
     2 hours 

 EduOLC
 Print
 Add Notes
 Bookmark this page