Swing
JRadio Button
Radio button allows user to
select only one radio button in each group at a time. If a radio button is
selected, whatever the previous radio button is, it will be deselected. To work
with radio buttons, we need two classes. First, we create the radio buttons with
the JRadioButton class,
whose constructors and methods are at the link below
"Swing Method & Constructor Table ." Then we create a group for the buttons with
the ButtonGroup class.
We must add the radio button themselves to a panel ( so they are displayed ) and
to a button group. Here is the usual way of creating a radio button.
JRadioButton pepsi, coke, tea;
Then in the frame constructor, we
call the JRadioButton constructor to create the radio button:
pepsi = new JRadioButton("Pepsi");
Now we would add the radio button
to the panel the usual way. Finally, to create a button group to group radio
buttons that work together, we would just call the ButtonGroup class
constructor.
ButtonGroup group = new
ButtonGroup();
Then add the method of the
ButtonGroup to add each radio button to the group
group.add(pepsi);
group.add(coke);
group.add(tea);
ClickHere to download RadioButton.java
Check Box
A
check box
is a control that the user can click to either check or uncheck. Check box is
similar to radio buttons, and it's usually used to let the user specify
Yes or No
to an option. To create a check box, we use the JCheckBox class, then adds it to
the panel.
JCheckBox ice_cream, banana,
ribs;
The above statement declares the
class variable to reference to the check box component. Then we can use the
constructor from JCheckBox to create the check boxes and add them to the panel.
ice_cream = new JCheckBox("Ice
Cream");
JPanel panel = new JPanel();
panel.add(ice_cream);
Notice that the above statements,
we didn't specify which box is initially checked, so as a result they're
initially unchecked. If we want to create a check box that is initially checked,
we would create a constructor like the following:
ice_Cream = new JCheckBox("Ice
Cream", true);
The same idea works with Radio
Buttons.
ClickHere to download CheckBox.java
The example above are all basic example on how to
create JCheckBox and JRadioButton, although it isn't much of a use. Hence, after
we learn about different layouts we will see more on how to use JCheckBox and
JRadioButtons to do more interesting things.
Borders
A
border
is a decorative element that visually
groups components by drawing a line around them. We can apply a border to any
object that inherits JComponent. To create a border we can call one of the
static methods from the BorderFactory methods which listed in " Swing Methods &
Constructor Table. "
To create a border and add to the panel
JPanel panel = new JPanel();
Border b = BorderFactory.createTitledBorder("Title");
panel.setBorder(b);
Hence, any components we add to the panel appear
within this border.

It might be a bit hard to notice, but if we look
at Title, there is a little faded line with the word "Title" in between the
lines.
ClickHere to download BevelBorder.java
Now lets combine JCheckBox, JRadioButton, and
BevelBorder together to create a ordering program. It's pretty long so bear with
me.

ClickHere to download Starbucks.java
The above program we created a main panel to hold
the JCheckBox and JRadioButton panel. The idea of the above program is to put
two panels inside another panel.
Click here to continue
<Previous><Java
24><Home><Swing
Method & Constructor Table>
