datalogo
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

Creating a JCheckBox

In Swing, the JCheckBox allows the user to select, or un-select, an item which is displayed. Any number of boxes can be selected at one time. Each box displayed is one JCheckBox. A checkmark will be used to choose a box.

JCheckBox often uses the methods isSelected and setSelected.
reddot To select, or un-select, a box, use the setSelected(boolean) method.
reddot To check if a box is selected, use the isSelected() method.

The JCheckBox and the JRadioButton are subclasses of JToggleButton.
A toggle button is a two-state button which operates like a light switch, on or off.

Note: While a JCheckBox allows for the user to make multiple choices at one time, the JRadioButton allows the user only one choice at a time.

dividerdash

JCheckBox
dogframe

reddot JCheckBox() - creates unselected check box (no text, no image)
reddot JCheckBox(String text) - creates unselected check box with text
reddot JCheckBox(String text, boolean selected) - creates a check box with text and specifies whether selected or not selected.
reddot JCheckBoxl(Icon image) - creates unselected check box with an image (icon).
reddot JCheckBoxl(Icon image. boolean selected)
- creates a check box with an image and specifies whether selected or not selected.
reddot JCheckBoxl(String text, Icon image) - creates unselected check box with both text and image.

Beware The Icon image replaces the check "box" image. If you want the image to appear AFTER the check "box", place the image in a JLabel and place it after the check "box".
dividerdash

These first code example below is only "displaying" the JCheckBox options shown above.
No event coding is generated in this example.

Note: In this example, one of the checkboxes is "pre-checked". Such a choice is at the
discretion of the programmer, and is most likely NOT a desired option in most cases.
It is used here for coding demonstration only.

Display CheckBoxes Only:
 

cbonly

dividerdash

Activate CheckBox:
 

When creating interactive components, the interface ActionListener will be needed so that the event created by the check (in this example) can be activated. Remember that import java.awt.event.*; will be needed.
(Note: When creating interactive checkboxes, the interface ItemListener may also be used.)

reddot Steps to Establishing an Active CheckBox:
1. Indicate the class "implements ActionListener"
2.
Declare (name) a checkbox variable
3. Create a
"new" checkbox and give it a label
4.
Attach "action" to the checkbox ("addActionListener(this)")
5.
Add checkbox to the Jpanel
6. Establish
action to be taken method ("actionPerformed")

Remember that when a JCheckBox is checked,
an event will be generated and something will happen.

dividerdash

Example: A message is displayed as checkboxes "Soda" and "Fries" are checked and unchecked. Four possible choices can appear: "Soda: chosen"; "Soda: not chosen"; "Fries: chosen"; "Fries: not chosen".
soda1  soda2

sodafries
cbx3b cbx3x

divider
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

Notice: These materials are free for your on-line use at this site, but are not free for the taking.
Please do not copy these materials or re-post them to the Internet, as it is copyright infringement.
If you wish hard-copies of these materials, refer to our subscription area, JavaMathBits.com.
Help us keep these resources free. Thank you.