Swing
JCheckBox
This
tutorial describes the Swing JCheckBox class.
The
JCheckBox
Class
Plain
JCheckBox
Image
JCheckBox
Other
JCheckBox
Methods
The
JCheckBox Class
A
checkbox exists to allow the user to select or deselect an option. You can
use the JCheckBox
class to implement checkboxes. You should not that you can also put check boxes in menus
using the JCheckBoxMenuItem class.
There
are no less than seven constructors that can be used to create a JCheckBox
because you have the choice of creating a checkbox with a plain box area or a
checkbox where the boxed area is replaced by an image. Here are the
constructors:-
|
Creates
a checkbox with no text, no icon, unselected
|
|
Creates
a checkbox with the specified text, no icon, unselected
|
|
Creates
a checkbox with the specified text, no icon, and selected if b is true
|
|
Creates
a checkbox with no text, the specified image, unselected
|
|
Creates
a checkbox with no text, the specified image, and selected if b is true
|
|
Creates
a checkbox with the specified text, the specified image, unselected
|
|
Creates
a checkbox with the specified text, the specified image, and selected if b is true
|
Plain
JCheckBox
To
create a plain unselected checkbox, use a line of code such as:-
JCheckBox
cloudCheckBox = new JCheckBox("Cloud");
If
you want to create a selected checkbox you can create it like this:-
JCheckBox
cloudCheckBox = new JCheckBox("Cloud", true);
Alternatively,
you can set the selected status using :-
//Set
box to be selected
cloudCheckBox.setSelected
(true);
//Set
box to be unselected
cloudCheckBox.setSelected
(false);
The
applet below shows two checkboxes. The first one was created with the selected
status set to true . The second was created with the selected
status set to false .
~~~Applet~~~
Image
JCheckBox
We can specify images to be used in place of the standard
selected and unselected box. To do this we need to create an Icon . We can use
the ImageIcon class for this:-
ImageIcon
imgUnselected = new ImageIcon (getImage
(getCodeBase(),"unselected.gif"));
then we can
use the variable that references the ImageIcon object in the
argument for the checkbox's constructor:-
JCheckBox
cloudCheckBox = new JCheckBox("Cloud",imgUnselected);
The
image specified at checkbox creation time is the image that will be used for
the unselected state. This means we have to specify another image to be
used when the checkbox is in the selected state. To do this use code
such as:-
ImageIcon
imgSelected = new ImageIcon (getImage
(getCodeBase(),"selected .gif"));
cloudCheckBox.setSelectedIcon(imgSelected);
Don't
forget to place your image file in the same folder as you class file.
The
applet below shows two checkboxes. I have specified an image of a tick
and an image of a cross to be used in place of the standard selected and unselected
box.
~~~Applet~~~
Important:
-- Using images in applets? Then read this
to understand the lines of code like:-
ImageIcon
imgUnselected
= new ImageIcon (getImage
(getCodeBase(),"unselected.gif"));
Don't forget
to import the package java.net.*
|
Other
JCheckBox Methods
Here are some
methods we can use with the JCheckBox class. You should note
that this is not a full list, there are lots of other methods.
JCheckBoc inherits
a lot of these methods from JComponent and AbstractButton . You
should check out the inherited JComponent
and JAbstractButton
class methods.
|
Set
the text displayed by the checkbox.
|
|
Set
the horizontal position of the checkbox text relative to the checkbox image
using as the argument: JCheckBox.LEFT, JCheckBox.CENTER or
JCheckBox.RIGHT .
|
|
Set
the vertical position of the checkbox's text relative to the checkbox's image using
as the argument:
JCheckBox.TOP, JCheckBox.CENTER or JCheckBox.BOTTOM .
|
|
Sets
the checkbox to selected if b is true else set it to unselected .
|
|
Returns
true if the checkbox is selected and false if is unselected .
|
|
Sets
the image to be used when the checkbox is in the selected state.
|
|
Use
setRolloverIcon(Icon m)
with setRolloverEnabled(true) to
make the checkbox display the specified icon when the cursor passes over it.
|
~Now
try the activity~
Activity A |
Using appropriate
methods from the JCheckBox , JAbstractButton and JComponent class,
create an applet with ...
- at least one panel to contain some checkboxes
- two checkboxes similar to the first applet shown on this page
- set the font color of the checkbox labels to any color of your choice, (apart from black).
- set the font of the checkbox labels to Times, italic and 10 points in size. The
Font class is explained
in the Graphics tutorial.
- set the panel background color to any color of your choice
|
|