Java's Basic User Interface Components - The Checkbox Class
(Page 5 of 10 )
Check Boxes are labeled or unlabeled boxes that can be either “Checked off” or “Empty”. Typically, they are used to select or deselect an option in a program, such as the “Disable sound” check boxes from a Windows screen.
Check Boxes are generally
nonexclusive, which means that if you have six check boxes in container, all the six can either be checked or unchecked at the same time. This component can be organized into check box group, which is sometimes called radio buttons. Both kinds of check boxes are created using the Checkbox class. To create a nonexclusive check box you can use one of the following constructors:
Checkbox() creates an unlabeled checkbox that is not checked.Checkbox(String) creates an unchecked checkbox with the given label as its string.
After you create a checkbox object, you can use the setState(boolean) method with a true value as argument for checked checkboxes, and false to uncheck it. Six checkboxes are created in Example 4, which is an applet to enable you to select up to six courses at a time. All five checkboxes are unchecked only the second option is checked.
Example 4
/*
<Applet code= “CheckboxTest.class”
Width = 200
Height = 150>
</applet>
*/
Import java.awt.*;
Public class CheckboxTest extends java.applet.Applet
{
Checkbox c1 = new Checkbox (“Java”);
Checkbox c2 = new Checkbox (“XML”);
Checkbox c3 = new Checkbox (“VB”);
Checkbox c4 = new Checkbox (“Oracle”);
Checkbox c5 = new Checkbox (“SQL”);
Checkbox c6 = new Checkbox (“ASP”);
Public void init(){
Add(c1);
c2.setStatus(true);
add(c2);
add(c3);
add(c4);
add(c5);
add(c6);
}
}
Next: The CheckboxGroup Class >>
More Java Articles
More By Gayathri Gokul