Java Reference
In-Depth Information
JCheckBox and JRadioButton
A JCheckBox is a GUI component that has an on state and an off state. The on
state is true and the off state is false . It is considered a button (a class
AbstractButton is defined in the Swing API from which JButton , JCheckBox ,
and JRadioButton are all derived). A JRadioButton is similar to a check box,
except that JRadioButton s are round; we use check box as a generic term to
describe both. Figure B.1 contains four check box objects. In this figure, the
Fill check box is currently true and the three other check boxes are in a
ButtonGroup : Only one check box in the group of three may be true. When a
check box in a group is selected, all the others in the group are deselected.
A ButtonGroup is constructed with zero parameters. Note that it is not a
Component ; it is simply a helper class that extends Object .
The common methods for JCheckBox are similar to JRadioButton and are:
A check box is a
GUI component
that has an on state
and an off state. A
ButtonGroup can
contain a set of
buttons in which
only one may be
true at a time.
JCheckBox( );
JCheckBox( String theLabel );
JCheckBox( String theLabel, boolean state );
boolean isSelected( );
void setLabel( String theLabel );
void setSelected( boolean state );
A stand-alone JCheckBox is constructed with an optional label. If a label is
not provided, it can be added later with setLabel . setLabel can also be used to
change the existing JCheckBox label. setSelected is most commonly used to set a
default for a stand-alone JCheckBox . isSelected returns the state of a JCheckBox .
A JCheckBox that is part of a ButtonGroup is constructed as usual and is
then added to the ButtonGroup object by use of the ButtonGroup add method.
The ButtonGroup methods are
ButtonGroup( );
void add( AbstractButton b );
Canvases
In the AWT, a Canvas component represents a blank rectangular area of the
screen onto which the application can draw. Primitive graphics are described
in Section B.3.2. A Canvas could also receive input from the user in the form
of mouse and keyboard events. The Canvas was never used directly: Instead,
the programmer defined a subclass of Canvas with appropriate functionality.
The subclass overrode the public method
A Canvas compo-
nent represents a
blank rectangular
area of the screen
onto which the
application can
draw or receive
input events.
void paint( Graphics g );
 
Search WWH ::




Custom Search