11.10. Adding Checkboxes to a Form
Problem
You want to add checkboxes to your form.
Solution
Use checkbox UI components.
Discussion
You should use checkboxes in situations in which you want a user to be able to select multiple options from a list, but you do not wish to use a list box. Or, alternatively, you should use a checkbox in any situation that calls for a simple yes/no or true/false answer. Checkboxes function similarly to radio buttons, except for a few important distinctions:
You cannot set a data value for a checkbox. Checkboxes have only two possible values:
true(checked) orfalse(unchecked).Checkboxes do not have a natively available group as radio buttons do.
Multiple checkboxes should not be used for mutually exclusive choices.
You can set the label of a checkbox with the setLabel(
) method, and you can set the placement of the label
(right or left) using the setLabelPlacement( )
method, just as with radio buttons.
The fact that checkboxes don’t provide native support for grouping, as radio buttons do, is not necessarily a problem. However, by adding grouping functionality to checkboxes (and coding the checkbox group class to inherit from the radio button group class), you can easily leverage the resizing and positioning code from Recipe 11.7 and Recipe 11.8 for radio button groups to work with checkboxes.
You can create a custom checkbox group class as follows:
Create a
FCheckBoxGroupClassconstructor.Designate
FCheckBoxGroupClassto inherit fromFRadioButtonGroupClass ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access