Class 1, Part 2 |
Intro to Java 30-IT-396 |
|
JCheckBox
-
When possible, you should give users a choice from a pre-defined list,
rather than allow them to type text freeform.
-
When users type text freeform, they tend to make mistakes, like misspellings.
-
It is also hard to match input. Imagine asking people what school
they attend. There are many different variations (and, unfortunately,
misspellings) for the University of Cincinnati!
-
Further, clicking a box is faster than typing text.
-
A JCheckBox is a Swing component that comes with a label and has an on/off
position. For a group of JCheckBoxes, multiple JCheckBoxes can be
checked at the same time. This would be ideal for something like
the toppings for a pizza, where the user can select none, all, or anywhere
in between.
-
Creating a JCheckBox is pretty easy. Just specify the label value
in the constructor, and then add it to the constructor.
JCheckBox chkCheese = new JCheckBox("Cheese");
pnlToppings.add(chkCheese);
-
The isSelected() method of JTextBox returns true if the JTextBox
is selected (or on), false if it is not.
-
You can add a listener to a JCheckBox with its addActionListener(ActionListener
l) method. Then, in your ActionListener object, have the logic
to be peformed when the JCheckBox is checked in the actionPerformed(ActionEvent
e) method implementation.
-
Or, just do it graphically with Forte'.
JRadioButton



Created by: Brandan
Jones January 4, 2002