JCheckBox


Summary:

JCheckBox is a class that allows developers to add check boxes to their UI. JCheckBox inherits from the abstract button class, so they have most of the characteristics that are associated with a button. In a group of check boxes, any number of then can be selected, thus allowing for a variety of options that can be included in a program. Check boxes have an item and action event that is associated with each one, this helps in identifying which box was check and provides that capability of multiple boxes checked simultaneously. Check boxes are primarily used in UI’s to give the user the ability to select options/characteristics that they want to have in the application while it is running.

Where to Find It:

JCheckBox can be found in the javax.swing library (javax.swing.JCheckBox)
A link to the Java API for this component is provided below.

The Basics:

The JCheckBox class has eight constructors. Each one can be used to initialize the check box with different kinds of properties. Here are the most common ways of initializing a JCheckBox:

Using the default constructor:
JCheckBox aCheckBox  =  new JCheckBox(); à This is the default way in which to create the check box

Using the constructor which takes a String:
JCheckBox aCheckBox  =  new JCheckBox( “Option1” ); à This creates the check box with the corresponding text being displayed beside the check box

Using the constructor which takes a String and a Boolean:
JCheckBox aCheckBox  =  new JCheckBox( “Option1”, true); à This creates the check box with the corresponding text being displayed beside the check box, as well as has this check box selected.

Customization:

A JCheckBox can be created to have a corresponding string, icon, or both. This is achieved by choosing the appropriate constructor when initializing the check box.

Action listeners can be added to a JCheckBox in the same manner that they are added to any other swing component. Adding action listeners allows you to perform a task when the check box is check or unchecked

Keyboard shortcuts can be applied to a check box. By adding a shortcut, the user is able to select/ unselect the box by pressing alt + <shortcut key>. Doing this allows you to make the interface quicker to used for someone who like keyboard shortcuts. Here is an example of how to add a keyboard short cut:

** Initialize the checkbox **
JCheckBox aCheckBox  =  new JCheckBox( “Option1” );

** Now add the keyboard shortcut **
aCheckBox.setMnemonic(KeyEvent.VK_C);

This will now allow the user to press alt + C to select and unselect the check box

Best Practices:

Check boxes are generally used to allow the user to accept a statement (i.e. terms of service), as well as for allowing the user to select one or more options.

When using check boxes for selecting options, it is good to limit the number of boxes that are available. If there are too many boxes, the UI can become over crowded and confusing for the user. If there are too few, you may not be able to give the user all the options that they want to have. Finding the balance can be a bit tricky, and depends on the application and what the options are for.

Limitations:

The only limitation for a JCheckBox, is that it allows a user to select multiple ones, so if you want a user to only select one item, using multiple checkboxes is not the way to go. For that scenario, radio buttons would be better, since they inherently allow the user to select on one option from a group.

Things to Watch Out for When Including JCheckBox:

When implementing a JCheckBox, the big thing to watch out for is putting action listeners on the check box. The reason that this is something to be aware of is that the action tied to the listener will be fired each and every time the user decides to select/ unselect. This can cause problems if the action being performed requires lots of computation, and the users is selecting the check box often.

References:

Other Helpful Resources: