16.4. The HTML File and the applet Tag

 
[Page 501]

15.4. Radio Buttons

Radio buttons , also known as option buttons , enable you to choose a single item from a group of choices. In appearance radio buttons resemble check boxes, but check boxes display a square that is either checked or blank, whereas radio buttons display a circle that is either filled (if selected) or blank (if not selected).

JRadioButton inherits AbstractButton and provides several constructors to create radio buttons, as shown in Figure 15.13. These constructors are similar to the constructors for JCheckBox .

Figure 15.13. JRadioButton defines a radio button.

Here is an example of a radio button with text Student, red foreground, white background, mnemonic key 'S', and initially selected.

 JRadioButton jrb =   new   JRadioButton(   "Student"   ,   true   ); jrb.setForeground(Color.red); jrb.setBackground(Color.white); jrb.setMnemonic(   'S'   ); 

To group radio buttons, you need to create an instance of java.swing.ButtonGroup and use the add method to add them to it, as follows :

 ButtonGroup group =   new   ButtonGroup(); group.add(jrb1); group.add(jrb2); 

This code creates a radio button group for radio buttons jrb1 and jrb2 so that jrb1 and jrb2 are selected mutually exclusively. Without grouping, jrb1 and jrb2 would be independent.

Note

ButtonGroup is not a subclass of java.awt.Component , so a ButtonGroup object cannot be added to a container.


When a radio button is clicked (selected or deselected), it fires an ItemEvent and then an ActionEvent . To see if a radio button is selected, use the isSelected() method.

Listing 15.4 gives a program that adds three radio buttons named Red , Green , and Blue into the preceding example to let the user choose the color of the message, as shown in Figure 15.14.

Figure 15.14. Three radio buttons are added to specify the color of the message.
(This item is displayed on page 502 in the print version)

Again there are at least two approaches to writing this program. The first is to revise the preceding CheckBoxDemo class to insert the code for adding the radio buttons and processing their events. The second is to create a subclass that extends CheckBoxDemo . Listing 15.4 gives the code to implement the second approach.


[Page 502]
Listing 15.4. RadioButtonDemo.java
(This item is displayed on pages 502 - 503 in the print version)


[Page 503]

RadioButtonDemo extends CheckBoxDemo and adds three radio buttons to specify the message color. When a radio button is clicked, the radio button's action event listener sets the corresponding foreground color in messagePanel .

The keyboard mnemonics 'R' and 'B' are already set for the Right button and Bold check box. To avoid conflict, the keyboard mnemonics 'E', 'G', and 'U' are set on the radio buttons "Red", "Green", and "Blue", respectively (lines 34 “36).

The program creates a ButtonGroup group and puts three JRadioButton instances ( jrbRed , jrbGreen , and jrbBlue ) in the group (lines 28 “31).

A radio button fires an ActionEvent and an ItemEvent when it is selected or deselected. You could process either the ActionEvent or the ItemEvent to choose a color. The example processes the ActionEvent . Please rewrite the code using the ItemEvent as an exercise.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net