How to Use Radio Buttons

 < Day Day Up > 

How to Use Radio Buttons

Radio buttons are groups of buttons in which, by convention, only one button at a time can be selected. The Swing release supports radio buttons with the JRadioButton [111] and ButtonGroup [112] classes. To put a radio button in a menu, use the JRadioButtonMenuItem [113] class. Other ways of displaying one-of-many choices are combo boxes and lists. Radio buttons look similar to check boxes, but, by convention, check boxes place no limits on how many items can be selected at a time.

[111] JRadioButton API documentation: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JRadioButton.html.

[112] ButtonGroup API documentation: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/ButtonGroup.html.

[113] JRadioButtonMenuItem API documentation: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JRadioButtonMenuItem.html.

Because JRadioButton inherits from AbstractButton , Swing radio buttons have all the usual button characteristics, as discussed earlier in How to Use Buttons (page 156). For example, you can specify the image displayed in a radio button.

Figure 45 is a picture of an application that uses five radio buttons to let you choose which kind of pet is displayed.

Figure 45. The RadioButtonDemo application.

graphics/07fig45.jpg

Try This:

  1. graphics/cd_icon.gif

    Run RadioButtonDemo using Java Web Start or compile and run the example yourself. [114]

    [114] To run RadioButtonDemo using Java Web Start, click the RadioButtonDemo link on the RunExamples/ components .html page on the CD. You can find the source files here: JavaTutorial/uiswing/components/example-1dot4/index.html#RadioButtonDemo .

  2. Click the Dog button or press Alt-D. The Dog button becomes selected, which makes the Bird button become unselected . The picture switches from a bird to a dog. This application has one action listener that listens to all the radio buttons. Each time the action listener receives an event, the application displays the picture for the radio button that was just clicked.

Each time the user clicks a radio button (even if it was already selected), the button fires an action event. One or two item events also occurone from the button that was just selected and another from the button that lost the selection (if any). Usually, you handle radio button clicks using an action listener.

Below is the code from RadioButtonDemo.java that creates the radio buttons in the previous example and reacts to clicks.

  //In initialization code:  //Create the radio buttons.     JRadioButton birdButton = new JRadioButton(birdString);     birdButton.setMnemonic(KeyEvent.VK_B);     birdButton.setActionCommand(birdString);     birdButton.setSelected(true);     JRadioButton catButton = new JRadioButton(catString);     catButton.setMnemonic(KeyEvent.VK_C);     catButton.setActionCommand(catString);     JRadioButton dogButton = new JRadioButton(dogString);     dogButton.setMnemonic(KeyEvent.VK_D);     dogButton.setActionCommand(dogString);     JRadioButton rabbitButton = new JRadioButton(rabbitString);     rabbitButton.setMnemonic(KeyEvent.VK_R);     rabbitButton.setActionCommand(rabbitString);     JRadioButton pigButton = new JRadioButton(pigString);     pigButton.setMnemonic(KeyEvent.VK_P);     pigButton.setActionCommand(pigString);     //Group the radio buttons.     ButtonGroup group = new ButtonGroup();     group.add(birdButton);     group.add(catButton);     group.add(dogButton);     group.add(rabbitButton);     group.add(pigButton);     //Register a listener for the radio buttons.     birdButton.addActionListener(this);     catButton.addActionListener(this);     dogButton.addActionListener(this);     rabbitButton.addActionListener(this);     pigButton.addActionListener(this); .. public void actionPerformed(ActionEvent e) {     picture.setIcon(new ImageIcon("images/"                      + e.getActionCommand() + ".gif")); } 

For each group of radio buttons, you need to create a ButtonGroup instance and add each radio button to it. The ButtonGroup takes care of unselecting the previously selected button when the user selects another button in the group.

You should generally initialize a group of radio buttons so that one is selected. However, the API doesn't enforce this rulea group of radio buttons can have no initial selection. Once the user has made a selection, exactly one button is selected from then on. There's no supported API for unselecting all the buttons.

Version Note: In versions prior to 1.4, invoking setSelected(null, true) on the ButtonGroup happened to deselect all the buttons. It no longer does so.


The Radio Button API

Tables 62 and 63 list the commonly used radio button- related API. Also consult the tables in The Button API (page 160) for methods inherited from AbstractButton . Other methods you might call, such as setFont and setForeground , are listed in the API tables in The JComponent Class (page 53). Also refer to the API documentation for JRadioButton and JRadioButtonMenuItem at:

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JRadioButton.html

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JRadioButtonMenuItem.html

Table 62. Radio Button Constructors

Constructor

Purpose

 JRadioButton(Action) JRadioButton(String) JRadioButton(String,              boolean) JRadioButton(Icon) JRadioButton(Icon, boolean) JRadioButton(String, Icon) JRadioButton(String, Icon,              boolean) JRadioButton() 

Create a JRadioButton instance. The string argument specifies the text, if any, that the radio button should display. Similarly, the Icon argument specifies the image that should be used instead of the look and feel's default radio button image. Specifying the boolean argument as true initializes the radio button to be selected, subject to the approval of the ButtonGroup object. If the boolean argument is absent or false, then the radio button is initially unselected. The JRadioButton(Action) constructor was introduced in 1.3.

 JRadioButtonMenuItem(Action) JRadioButtonMenuItem(String) JRadioButtonMenuItem(Icon) JRadioButtonMenuItem(String, Icon) JRadioButtonMenuItem() 

Create a JRadioButtonMenuItem instance. The arguments are interpreted in the same way as the arguments to the JRadioButton constructors, except that any specified icon is shown in addition to the normal radio button icon. The JRadioButtonMenuItem(Action) constructor was introduced in 1.3.

Table 63. Commonly Used Button Group Constructors and Methods

Constructor or Method

Purpose

ButtonGroup()

Create a ButtonGroup instance.

 void add(AbstractButton) void remove(AbstractButton) 

Add a button to the group, or remove a button from the group.

 public ButtonGroup getGroup()  (in  DefaultButtonModel  )  

Get the ButtonGroup , if any, that controls a button. For example:

 ButtonGroup group =               ((DefaultButtonModel)                 button.getModel()).getGroup(); 

Introduced in 1.3.

Examples That Use Radio Buttons

The following examples use radio buttons.

Example

Where Described

Notes

DialogDemo

How to Make Dialogs (page 187)

Has Show it buttons whose behavior is tied to the state of radio buttons. Uses sizable , though anonymous, inner classes to implement the action listeners.

RadioButtonDemo

This section

Uses radio buttons to determine which of five images it should display.

MenuDemo

How to Use Menus (page 277)

Contains radio button menu items and check box menu items.

 < Day Day Up > 


JFC Swing Tutorial, The. A Guide to Constructing GUIs
The JFC Swing Tutorial: A Guide to Constructing GUIs (2nd Edition)
ISBN: 0201914670
EAN: 2147483647
Year: 2004
Pages: 171

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