Class 1, Part 2 |
Intro to Java 30-IT-396 |
|
JRadioButton
-
There are many times when you want the user to pick only one option out
of many. In these instances, use a JRadioButton.
-
You must define a ButtonGroup that associates the JRadioButtons together.
For each ButtonGroup, only one RadioButton can be selected at a time.
But, you can have multiple ButtonGroups in one container, so there can
be multiple groups of RadioButtons.
-
The first argument to the JRadioButton constructor should be the text for
the label, and the second argument is true if you want the button
to be checked, or false if not.
-
Example:
ButtonGroup grpCrust = new ButtonGroup();
JRadioButton rdoThin = new JRadioButton("Thin Crust", true);
grpCrust.add(rdoThin);
JRadioButton rdoTraditional = new JRadioButton("Traditional", false);
grpCrust.add(rdoTraditional);
JRadioButton rdoPan = new JRadioButton("Pan", false);
grpCrust.add(rdoPan);
-
It is often a good idea to keep JRadioButtons of the same group together
in the same panel. This is logical for the user.
-
We can add action listeners just as we have in previous swing components.
We need to create the ActionListener class, instantiate it, and add it
with the addActionListner(ActionListener l) method.
-
And, of course, we can use the GUI editor in Forte' to do a lot of the
work for us.
JComboBox



Created by: Brandan
Jones January 4, 2002