Check Boxes
Check boxes are used as on-off or yes-no switches since every time you click on one, you toggle to the opposite selection. You are able to select as many or as few check boxes as you like. Then when you write your program, the state of each check box can be compared to determine an action. A button is needed to trigger an event to check the state of the check boxes.
The following line will create a checkbox with 'Apples' as a label, null as a place holder for a group argument, and false to indicate it is not selected. (The group argument is used for radio buttons which are a special kind of check boxes.)
Checkbox apples = new Checkbox("Apples", null, false);
Once you create the checkbox just add it to the applet (or other container) by entering the command
add(apples);
where apples is the Checkbox name. We can change the Checkbox label or get the Checkbox text using the Checkbox.setLabel(String) or Checkbox.getLabel( ) methods. The getState( )method returns true of false, depending on whether the Checkbox is selected or unselected. The setState(boolean) method changes the Checkbox's state to true (for selected) or false(for unselected.)
Consider the example below which illustrates how Checkboxes can be implemented.
The Applet below prints a list of fruits. Check as many or few as you like. The "Done" button triggers an event in order to check which boxes were selected. A list of selected items is then painted on the applet window.
| The four fruit check boxes are added to
the applet window. A button is added also. Select any or all of the check boxes. Click on "Done" when finished. A shopping list is painted in the applet window. |
The code for the Applet is listed below
import java.applet.*;
import java.awt.*;
public class checkbox extends Applet
{
Checkbox apples =
new Checkbox("Apples",null,false);
Checkbox bananas =
new Checkbox("Bananas",null,false);
Checkbox grapes =
new Checkbox("Grapes",null,false);
Checkbox peaches =
new Checkbox("Peaches",null,false);
Font f = new Font
("TimesRoman",Font.ITALIC,14);
public void init()
{
add(apples);
add(bananas);
add(grapes);
add(peaches);
add(new Button("Done"));
}
|
We import all the facilities of the AWT
and applet that Java has to offer by using the *
wildcard. Create an applet. Create and initialize four check boxes with fruit labels. Initialize font f as TimesRoman size 14. This is done here so that the variables are declared globally and recognized in all other methods. In the init method, add the new check boxes to the applet window and include a "Done" button also. |
public boolean action
(Event evt, Object whatAction)
{
if(!(evt.target
instanceof Button))
{
return false;
} else {
repaint();
return true;
}
}
|
When an action takes place the default
handleEvent method calls the action method of our
component. If its not a button event we simply return |
public void paint (Graphics g)
{
g.setFont(f);
g.setColor(Color.blue);
if (apples.getState())
g.drawString("Apples",50,60);
if (bananas.getState())
g.drawString("Bananas",50,80);
if (grapes.getState())
g.drawString("Grapes",50,100);
if (peaches.getState())
g.drawString("Peaches",50,120);
}
}
|
Create an instance, g, of the Graphics class. Set attributes of the object g. Use the getState method to determine which check boxes were selected and print the appropriate fruits. |
The easiest way to obtain the code for this Applet is to click here and copy the contents of the screen that loads in your browser , paste it in a text editor and save it in a file called "checkbox.java".
After compiling this program we need to create an HTML page with a reference to the class file. It will require a graphic window that is large enough to contain the check boxes, button, and the full list of fruits.
Save the HTML code in the same directory as the class file.
When you load the HTML file in your browser the applet will execute as below.
Back to the Java AWT Components Page