5.5 JRadionButton class
We can use JRadioButton to create a mutually exclusive component group. Technically speaking, although we can put a set of JCheckBox components in a ButtonGroup and only one component can be selected at a time, they do not seem correct. The JRadioButton and JCheckBox components are different, as shown in 5-10. The differences in appearance tell end users to expect specific behavior of components.
Package net. ariel. ch05;
Import java. awt. Container;
Import java. awt. GridLayout;
Import javax. swing. BorderFactory;
Import javax. swing. ButtonGroup;
Import javax. swing. JPanel;
Import javax. swing. JRadioButton;
Import javax. swing. border. Border;
Public class RadioButtonUtils {
Private RadioButtonUtils (){
}
Public static Container createRadioButtonGrouping (String elements [], String title ){
JPanel panel = new JPanel (new GridLayout (0, 1 ));
If (title! = Null ){
Border border = BorderFactory. createTitledBorder (title );
Panel. setBorder (border );
}
ButtonGroup group = new ButtonGroup ();
JRadioButton aRadioButton;
For (int I = 0, n = elements. length; inew JRadioButton (elements [I]);
Panel. add (aRadioButton );
Group. add (aRadioButton );
}
Return panel;
}
}
Now we can create a combination in a simpler way, as shown in the sample program in 5-6.
/**
*
*/
Package net. ariel. ch05;
Import java. awt. BorderLayout;
Import java. awt. Container;
Import java. awt. EventQueue;
Import javax. swing. JFrame;
/**
* @ Author mylxiaoyi
*
*/
Public class GroupRadio {
Private static final String sliceOptions [] = {
"4 slices", "8 slices", "12 slices", "16 slices"
};
Private static final String crustOptions [] = {
"Sicilian", "Thin Crust", "Thick Crust", "Stuffed Crust"
};
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
Runnable runner = new Runnable (){
Public void run (){
JFrame frame = new JFrame ("Grouping Example ");
Frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
Container sliceContainer = RadioButtonUtils. createRadioButtonGrouping (sliceOptions, "Slice Count ");
Container crustContainer = RadioButtonUtils. createRadioButtonGrouping (crustOptions, "Crust Type ");
Frame. add (sliceContainer, BorderLayout. WEST );
Frame. add (crustContainer, BorderLayout. EAST );
Frame. setSize (300,200 );
Frame. setVisible (true );
}
};
EventQueue. invokeLater (runner );
}
}
JRadioButton consists of several aspects. Similar to JToggleButton and JCheckBox, JRadioButton uses ToggleButtonModel to represent its data model. He uses the ButtonGroup to provide mutex combinations through AbstractButton, and the user interface delegate is RadioButtonUI.
Next we will discuss how to use JRadioButton in different ways.
5.5.1 create the JRadioButton component
Similar to JCheckBox and JToggleButton, JRadioButton has eight constructors:
public JRadioButton()JRadioButton aRadioButton = new JRadioButton(); public JRadioButton(Icon icon)JRadioButton aRadioButton = new JRadioButton(new DiamondIcon(Color.CYAN, false));aRadioButton.setSelectedIcon(new DiamondIcon(Color.BLUE, true)); public JRadioButton(Icon icon, boolean selected)JRadioButton aRadioButton = new JRadioButton(new DiamondIcon(Color.CYAN, false), true);aRadioButton.setSelectedIcon(new DiamondIcon(Color.BLUE, true)); public JRadioButton(String text)JRadioButton aRadioButton = new JRadioButton("4 slices"); public JRadioButton(String text, boolean selected)JRadioButton aRadioButton = new JRadioButton("8 slices", true); public JRadioButton(String text, Icon icon)JRadioButton aRadioButton = new JRadioButton("12 slices", new DiamondIcon(Color.CYAN, false));aRadioButton.setSelectedIcon(new DiamondIcon(Color.BLUE, true)); public JRadioButton(String text, Icon icon, boolean selected)JRadioButton aRadioButton = new JRadioButton("16 slices", new DiamondIcon(Color.CYAN, false), true);aRadioButton.setSelectedIcon(new DiamondIcon(Color.BLUE, true)); public JRadioButton(Action action)Action action = ...;JRadioButton aRadioButton = new JRadioButton(action);
Each one allows us to customize one or more labels, icons, or initial selected status attributes. Unless otherwise specified, there is no text in the label, and the default/unselected status icon of the check box is unselected. After creating a group of radio button components, we need to put each component in a ButtonGroup so that they can work normally. In the combination, only one button can be selected at a time. If we initialize the icon in the constructor, It is the icon in the unselected status of the check box, and the same icon is displayed when the check box is selected. We either use the setSelectedIcon (Icon newValue) method described in JCheckBox to initially select the Icon, or make sure that the Icon is status aware and automatically updated.
5.5.2 JRadioButton attributes
JRadioButton has two attributes that overwrite the parent class JToggleButton, as shown in table 5-5.
JRadioButton attributes
Attribute name
|
Data Type
|
Accessibility |
AccessibleContext
|
AccessibleContext
|
Read-Only |
UIClassID
|
String
|
Read-Only |
5.5.3 combine the JRadioButton component into a ButtonGroup
JRa