Tuesday, 4 September 2012

java program to use awt check box group in applet.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="CBGroup" width=250 height=200>
</applet>
*/
public class CBGroup extends Applet implements ItemListener
{
String msg = "";
Checkbox obj1, obj2, obj3, obj4;
CheckboxGroup cbg;

public void init()
{
cbg = new CheckboxGroup();
obj1 = new Checkbox("A", cbg, false);
obj2 = new Checkbox("B", cbg, false);
obj3 = new Checkbox("C", cbg, true);
obj4 = new Checkbox("D", cbg, false);
add(obj1);
add(obj2);
add(obj3);
add(obj4);
obj1.addItemListener(this);
obj2.addItemListener(this);
obj3.addItemListener(this);
obj4.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
// Display current state of the check boxes.
public void paint(Graphics g)
{
msg = "Current selection: ";
msg += cbg.getSelectedCheckbox().getLabel();
g.drawString(msg, 6, 100);
}
}
output: