Radio Buttons
Radio buttons or checkbox groups are special kinds of checkboxes. They look the same as checkboxes but within a particular group of them, only one can be selected at a time.
The following line will create a series of radio buttons named 'colors'.
CheckboxGroup colors = new CheckboxGroup();
Once you create the series of radio buttons, add the individual checkboxes. There are three arguments: the radio button label, the group, and true or false, depending on whether the button is selected or unselected. Only one radio button in the group can be selected.
add(new
Checkbox("Red",colors,false));
add(new Checkbox("White",colors,false));
add(new Checkbox("Green",colors,false));
add(new Checkbox("Blue",colors,false));
The same checkbox methods that
were presented on the checkbox page can be implemented with radio
buttons in the group. These include:
Checkbox.setLabel(String)
Checkbox.getLabel( )
getState( )
setState(boolean)
The methods getCheckboxGroup() and setCheckboxGroup() can be used to access and change the group of a given checkbox. To get or set the currently selected checkbox, use the getCurrent() or setCurrent(Checkbox) methods.
The example below implements radio buttons to change the background color of the applet window. Remember, only one radio button can be selected at a time.
The code for the Applet is listed below
import java.applet.*;
import java.awt.*;
public class radio extends Applet
{
public void init()
{
add(new Label("The 4 radio buttons will
change the screen color."));
CheckboxGroup colors = new CheckboxGroup();
add(new Checkbox("Red",colors,false));
add(new Checkbox("White",colors,false));
add(new Checkbox("Green",colors,false));
add(new Checkbox("Blue",colors,false));
add(new Label("Notice that you can only
select one radio button."));
add(new Label("And selecting a radio button
triggers an event"));
add(new Label("that we use to change the
screen color."));
}
|
We import all the facilities
of the AWT and applet that Java has to offer by using the
* wildcard. Create an applet. Create a checkbox group called colors. Add four check boxes with labels, in group colors, set as unselected. |
public boolean action(Event evt, Object
whichAction)
{
Checkbox currentCheckbox=(Checkbox)evt.target;
boolean checkboxState = currentCheckbox.getState();
if (currentCheckbox.getLabel() == "Red")
if (checkboxState)
{
setBackground(Color.red);
}
if (currentCheckbox.getLabel() == "White")
if (checkboxState)
{
setBackground(Color.white);
}
if (currentCheckbox.getLabel() == "Green")
if (checkboxState)
{
setBackground(Color.green);
}
if (currentCheckbox.getLabel() == "Blue")
if (checkboxState)
{
setBackground(Color.blue);
}
repaint();
return true;
}
}
|
When an event takes place the default
handleEvent method calls the event method of our
component. The whichAction parameter of the action method
will be an instance of a Boolean class that is true if
the checkbox was clicked on and false if the checkbox was
clicked off. Set checkboxState to the value of the currently selected radio button Use a series of if-thens to check each radio button until you find the one that is true (has been selected). Change the background color appropriately. |
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 "buttons.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 radio buttons and labels (width-301 and height-401).
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