Java Tutorial

AWT Check Box

Check box is basically used for maintaining checklist purpose checkbox is an interactive component which contains a square symbol and a label.

Creating a checkbox is nothing but creating an object of checkbox class

Check Box API:


 checkbox ();
checkbox (String);
checkbox (String, boolean);

Instance methods:

public void setLabel (String); 
public String getLabel (); 
public void setState (boolean); 
public boolean getState ();
public void addItemListener (ItemListener);
public void removeItemListener (ItemListener);

Write a java program which illustrates the concept of Checkbox?


import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
/*<applet code="CheckboxApp" height=200 width=200>
public class CheckboxApp extends Applet
    Checkbox cb; 
    Label l,l1;
    public void init ()
        cb1=new Checkbox ("Java"); 
        l=new Label ("Course: "); 
        l1=new Label ();
        add (cb);
        add (l);
        add (l1);
    public void Start ()
        cb1.addItemListener (new itl ());
    class itl implements ItemListener
        public void itemStateChanged (ItemEvent ie)
            Object obj=ie.getItemSelectable (); 
            Checkbox cb1= (Checkbox) obj;
            if (cb1.getState ())
                 l1.setText ("U HAVE SELECTED "+cb1.getLabel ());

                l1.setText ("U HAVE SELECTED NONE");