RadioButton is a Button that maintains a selection state exclusively within a specific ButtonGroup. Because RadioButton inherits from Button, radio buttons have all the usual button characteristics, as discussed in Button. For example, you can specify the image displayed in a radio button. Each time the user clicks a radio button (even if it was already selected), the button fires an action event, just as in Button.
To create a RadioButton. use:
RadioButton radioButton = new RadioButton(“Radio Button 1”);
Figure 1 shows the RadioButton this code produces.
Figure: RadioButton example
The ButtonGroup component manages the selected and unselected states for a set of RadioButtons. For the group, the ButtonGroup instance guarantees that only one button can be selected at a time.
Initially, all RadioButtons in a ButtonGroup are unselected. Each ButtonGroup maintains the selected index, and can get a specific RadioButton by calling getRadioButton(int index).
The following code snippet creates a button group made of three RadioButtons:
... Label inAGroup = new Label("Radio buttons in a group:"); RadioButton radioButton = new RadioButton("Radio button 1"); RadioButton radioButtonTwo = new RadioButton("Radio button 2"); RadioButton radioButtonThree = new RadioButton("Radio button 3"); ButtonGroup group = new ButtonGroup(); group.add(radioButton); group.add(radioButtonTwo); group.add(radioButtonThree); form.addComponent(inAGroup); form.addComponent(radioButton); form.addComponent(radioButtonTwo); form.addComponent(radioButtonThree); ...
The code snippet result is shown in Figure 2.
Figure: ButtonGroup with three RadioButtons