CheckBox

A checkbox is a control that is used to turn an option on & off.
Constructors
CheckBox( )
CheckBox(string)
CheckBox(string,Boolean)
Check Box(string,Boolean,checkBoxgroup)
CheckBox(string,checkBox,group,Boolean)
Methods
boolean getState( )
Void setState(Boolean)
String getLabel(string)
To handle the checkboxes we have to use the itemlistner interface.That interface defines the itemStateChanged method.That method having the argument itemevent.

 

WRITE A PROGRAM ON CHECK BOXES.

 import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Check extends Applet implements ItemListener
{
String s=" ";
Checkbox Win98,WinNt,Linux;
public void init()
{
Win98=new Checkbox("Windows98");
WinNt=new Checkbox("WindowsNt");
Linux=new Checkbox("Linux");
add(Win98);
add(WinNt);
add(Linux);
Win98.addItemListener(this);
WinNt.addItemListener(this);
Linux.addItemListener(this);
}
public void itemStateChanged(ItemEvent ae)
{
repaint();
}
public void paint(Graphics g)
{
s="current state: ";
g.drawString(s,16,80);
s="Windows98: "+Win98.getState();
g.drawString(s,20,100);
s="LinuX: "+Linux.getState();
g.drawString(s,6,120);
}
}

/*<applet code=Check width=300 height=130>
</applet>*/

                       

                       

 

 

WRITE A PROGRAM ON CHECK BOX BROUP.(IT IS LIKE RADIO BUTTONS)

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Rcheck extends Applet implements ItemListener
{
String s=" ";
Checkbox Win98,WinNt,Linux;
CheckboxGroup ch;
public void init()
{
ch=new CheckboxGroup ();
Win98=new Checkbox("Windows98",ch,true);
WinNt=new Checkbox("WindowsNt",ch,false);
Linux=new Checkbox("Linux",ch,false);
add(Win98);
add(WinNt);
add(Linux);
Win98.addItemListener(this);
WinNt.addItemListener(this);
Linux.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
s="Selected Item: ";
s+=ch.getSelectedCheckbox().getLabel();
g.drawString(s,6,120);
}
}

/*<applet code=Rcheck width=250 height=200>
</applet>*/