Radio Buttons
Introduction.
Radio Buttons are typically used when the
developer wants the user to select only one of the options in a list. For
example, say you are filling out a form that requires you to enter a
credit card. The radio buttons likely give
a list of the credit cards the site accepts. Radio buttons are distinctly different from check boxes in that they are grouped in such a way that the user can only select one of them. The following table contains a list of the constructors that are available for the JRadioButton class. |
Constructor | Description |
JRadioButton(String) | This will put the specified String as a label for the radio button |
JRadioButton(String, boolean) | This will put the specified String as a label for the radio button and initialize it as selected if the boolean argument is true |
JRadioButton(Icon) | This will put the specified icon as a label for the radio button |
JRadioButton(Icon, boolean) | This will put the specified icon as a label for the radio button and initialize it as selected if the boolean argument is true |
JRadioButton(String, Icon) | This will put the specified String as a label along with the Icon as a label |
JRadioButton(String, Icon, boolean) | This will put the specified String as a label along with the Icon as a label and initialize it as selected if the boolean argument is true |
These constructors should look very similar to those of
the checkboxes. But because of the issues associated with allowing
only one option in a group of radio buttons to be selected, we have one
additional things we need to do. We are going to quickly describe
ButtonGroups. The following table contains a typical code segment |
// after instantiating some
buttons with name newButtons // this is often done with an array of JRadioButtons ButtonGroup groupName = new ButtonGroup( ); // now go through the
radio buttons groupName.add(newButtons); |
This sort of thing will often be done using loops to go
through an array of JRadioButtons. The other thing we need to consider when making use of radio buttons is how the program can sense that the user has interacted with them. To reiterate a previous page we will make use of ItemListeners. Displaying What's Selected. Now we present our first example, an applet called TimeUnits.java. This program allows the user to check a particular radio button and which will cause a display to change. |
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class TimeUnits extends JApplet {
} // ends the TimeUnits class |
Since the size of the GUI has impact, here is TimeUnits.html. |
<html> <applet code="TimeUnits.class" width=100 height=200> </applet> </html> |
The following image represents the GUI. |
Notice how the words in the text box change as each
radio button is selected. You should also notice that only one radio
button can be selected. The code will be discussed in class. The only things that is particularly new is the ways it
Both of these have their own sections of code. Notice how the RadioButtonHandler class implements ItemListener. Changing Font Properties. We are now going to use a program that I modified from Deitel and Deitel that changes the properties of some text based on which button the user selects. It's got some fairly clever coding in it, though it's essentially a variation on what we did in the last webpage. You should call the application RadioButtonTest.java. |
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class RadioButtonTest extends JFrame {
} // ends RadioButtonTest class |
The following image represents the GUI. |
Notice how the displayed text changes whether it is italicized and/or bold based on the user selections. You should also notice how in this instance we needed more options in order to deal with limiting the user's choices. |