AWT CheckBox Class

  • Post author:
  • Post category:AWT
  • Post comments:0 Comments
CheckBox Class

Introduction

Checkbox class control is used to turn an option on(true) or off(false). There is a label for each checkbox representing what the checkbox does. The state of a checkbox can be changed by clicking on it.

Class declaration

Following is the declaration for java.awt.Checkbox class:

public class Checkbox
   extends Component
      implements ItemSelectable,Accessible

Class constructors

S.N.Constructor & Description
1Checkbox()Creates a check box with an empty string for its label.
2Checkbox(String label)Creates a check box with the specified label.
3Checkbox(String label, boolean state)Creates a check box with the specified label and sets the specified state.
4Checkbox(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.
5Checkbox(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.

Class methods

S.N.Method & Description
1void addItemListener(ItemListener l)Adds the specified item listener to receive item events from this check box.
2void addNotify()Creates the peer of the Checkbox.
3AccessibleContext getAccessibleContext()Gets the AccessibleContext associated with this Checkbox.
4CheckboxGroup getCheckboxGroup()Determines this check box’s group.
5ItemListener[] getItemListeners()Returns an array of all the item listeners registered on this checkbox.
6String getLabel()Gets the label of this check box.
7<T extends EventListener>T[] getListeners(Class<T> listenerType)Returns an array of all the objects currently registered as FooListeners upon this Checkbox.
8Object[] getSelectedObjects()Returns an array (length 1) containing the checkbox label or null if the checkbox is not selected.
9boolean getState()Determines whether this check box is in the on or off state.
10protected String paramString()Returns a string representing the state of this Checkbox.
11protected void processEvent(AWTEvent e)Processes events on this check box.
12protected void processItemEvent(ItemEvent e)Processes item events occurring on this check box by dispatching them to any registered ItemListener objects.
13void removeItemListener(ItemListener l)Removes the specified item listener so that the item listener no longer receives item events from this check box.
14void setCheckboxGroup(CheckboxGroup g)Sets this check box’s group to the specified check box group.
15void setLabel(String label)Sets this check box’s label to be the string argument.
16void setState(boolean state)Sets the state of this check box to the specified state.

Methods inherited

This class inherits methods from the following classes:

  • java.awt.Component
  • java.lang.Object

CheckBox Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > adglob > gui >AwtControlDemo.java

package com.adglob.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\adglob\gui\AwtControlDemo.java

If no error comes that means compilation is successful. Run the program using following command.

D:\AWT>java com.adglob.gui.AwtControlDemo

Verify the following output

Previous Topic:-Click Here

Leave a Reply