Java Reference
In-Depth Information
bold, italic, or bold and italic combined. The check boxes in our GUI are set up
to change the characteristics of our font style.
The style of a font is represented as an integer, and integer constants defined in
the Font class are used to represent the various aspects of the style. The constant
PLAIN is used to represent a plain style. The constants BOLD and ITALIC are used to
represent bold and italic, respectively. The sum of the BOLD and ITALIC constants
indicates a style that is both bold and italic.
The itemStateChanged method of the listener determines what the revised
style should be now that one of the check boxes has changed state. It initially
sets the style to be plain. Then each check box is consulted in turn using the
isSelected method, which returns a boolean value. First, if the bold check box
is selected (checked), then the style is set to bold. Then, if the italic check box is
selected, the ITALIC constant is added to the style variable. Finally, the font of
the label is set to a new font with its revised style.
Note that, given the way the listener is written in this program, it doesn't
matter which check box was clicked to generate the event. Both check boxes are
processed by the same listener. It also doesn't matter whether the changed check
box was toggled from selected to unselected or vice versa. The state of both check
boxes is examined if either is changed.
Radio Buttons
A radio button is used with other radio buttons to provide a set of mutually
exclusive options. Unlike a check box, a radio button is not particularly useful by
itself. It has meaning only when it is used with one or more other radio buttons.
Only one option out of the group is valid. At any point in
time, one and only one button of the group of radio buttons is
selected (on). When a radio button from the group is pushed,
the other button in the group that is currently on is automati-
cally toggled off.
The term “radio buttons” comes from the way the buttons
worked on an old-fashioned car radio. At any point, one but-
ton was pushed to specify the current choice of station; when another was pushed,
the current one automatically popped out.
The QuoteOptions program, shown in Listing 5.16, displays a label and a
group of radio buttons. The radio buttons determine which quote is displayed in
the label. Because only one of the quotes can be displayed at a time, the use of
radio buttons is appropriate. For example, if the Comedy radio button is selected,
the comedy quote is displayed in the label. If the Philosophy button is then
pressed, the Comedy radio button is automatically toggled off and the comedy
quote is replaced by a philosophical one.
KEY CONCEPT
Radio buttons operate as a group,
providing a set of mutually exclusive
options.
VideoNote
Examples using
check boxes and
radio buttons.
 
Search WWH ::




Custom Search