Java Reference
In-Depth Information
Here is an example for creating a check box with the text Student . Its foreground is red , the
background is white , its mnemonic key is S , and it is initially selected.
JCheckBox jchk = new JCheckBox( "Student", true );
jchk.setForeground(Color.RED);
jchk.setBackground(Color.WHITE);
jchk.setMnemonic( 'S' );
The button can also be accessed by using the keyboard mnemonics. Pressing Alt+S is
equivalent to clicking the check box.
To see if a check box is selected, use the isSelected() method.
mnemonics
isSelected?
12.31 How do you create a check box? How do you create a check box with the box
checked initially? How do you determine whether a check box is selected?
Check
Point
12.13 JRadioButton
To create a radio button, use the JRadioButton class.
Key
Point
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 12.23. These constructors are similar to the constructors for
JCheckBox .
javax.swing.AbstractButton
javax.swing.JToggleButton
javax.swing.JRadioButton
+JRadioButton()
+JRadioButton(text: String)
+JRadioButton(text: String, selected:
boolean)
+JRadioButton(icon: Icon)
+JRadioButton(text: String, icon: Icon)
+JRadioButton(text: String, icon: Icon,
selected: boolean)
Creates a default radio button without any text or icon.
Creates a radio button with text.
Creates a radio button with text and specifies whether the radio button is
initially selected.
Creates a radio button with an icon.
Creates a radio button with text and an icon.
Creates a radio button with text and an icon, and specifies whether the
radio button is initially selected.
F IGURE 12.23
JRadioButton defines a radio button.
Here is an example for creating a radio button with the text Student. The code specifies
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' );
 
 
 
Search WWH ::




Custom Search