Showing posts with label java check box group example. Show all posts
Showing posts with label java check box group example. Show all posts

Apr 19, 2012

Check Box Group


The checkbox group is also called as option button or radio button or exclusive check boxes. Check boxes are grouped together into check-box groups. So, only one member of a check box group is selected at a time. To use a check box group.
You first create an instance of CheckboxGroup and then pass the instance as a parameter to the Checkbox 

constructor:

Checkbox cb=new CheckBox(String caption, group, state)

Here group refers the CheckboxGroup object

CheckboxGroup cbg=new CheckboxGroup();

Example:

import java.applet.*;
import java.awt.*;

public class CheckboxGroupDemo extends Applet
{
    public void init()
    {
        setLayout(new GridLayout(3, 1));
        CheckboxGroup cbg=new CheckboxGroup();
        Checkbox cb1=new Checkbox("M.Sc", cbg,true);
        Checkbox cb2=new Checkbox("M.C.A", cbg, false);
        Checkbox cb3=new Checkbox("M.E", cbg, false);
        add(cb1);
        add(cb2);
        add(cb3);
    }
}
/*
 * <applet code="CheckboxGroupDemo.class" width=200 height=75>
 * </applet>
 */

How to Execute this Example??

d:\>  javac CheckboxGroupDemo.java
d:\>  appletviewer CheckboxGroupDemo.java

Output: