
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);
/*
The program demonstrate the use of JRadioButton
*
*/
import javax.swing.*;
public class RadioButton extends JFrame
{
JRadioButton pepsi, coke, tea;
JPanel panel = new JPanel();
public RadioButton()
{
// Sets the title
setTitle("Radio Button");
// Sets the window size
setSize(300, 400);
// Sets the action when the window
close
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a button group to group the
radio buttons together
ButtonGroup group = new ButtonGroup();
// Creates a radio button for pepsi
pepsi = new JRadioButton("Pepsi");
// Creates a radio button for coke
coke = new JRadioButton("Coke");
// Creates a radio button for Tea
tea = new JRadioButton("Tea");
// Add the radio button to the group
group.add(pepsi);
group.add(coke);
group.add(tea);
// Adds the button to the panel
panel.add(pepsi);
panel.add(coke);
panel.add(tea);
//
Adds the panel to the content pane
add(panel);
// Sets the window to be visible
setVisible(true);
}
public static void main(String[] args)
{
new RadioButton();
}
}
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.
/*
The program demonstrate a check box
It has an initial box checked
*
*/
import javax.swing.*;
public class CheckBox extends JFrame
{
JCheckBox ice_cream, banana, ribs;
JPanel panel;
public CheckBox()
{
//
Sets the title of the window
setTitle("Check Bow Window");
//
Sets the size of the window
setSize(300, 400);
// Sets the action when the window
closes
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//
Creates a panel
panel = new JPanel();
//
Creates 3 check box with ice_cream initially checked
ice_cream = new JCheckBox("Ice
Cream", true);
banana = new JCheckBox("Banana");
ribs = new JCheckBox("Ribs");
//
Adds the check boxes to the panel
panel.add(ice_cream);
panel.add(banana);
panel.add(ribs);
//
Adds the panel to the content pane
add(panel);
// Sets the window to be visible
setVisible(true);
}
public static void main(String[] args)
{
new CheckBox();
}
}
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.
/*
The program demonstrates bevel border
*
*/
import javax.swing.*;
import javax.swing.border.*;
public class BevelBorder extends JFrame
{
JPanel panel;
public BevelBorder()
{
// Sets the title of the window
setTitle("Border Window");
// Sets the size of window
setSize(300, 400);
// Sets the action when the window is
close
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Creates a panel
panel = new JPanel();
// Creates a border name title
Border b =
BorderFactory.createTitledBorder("Title");
panel.setBorder(b);
// Adds the panel to the content pane
add(panel);
// Sets the window to be visible
setVisible(true);
}
public static void main(String[] args)
{
new BevelBorder();
}
}
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.
/*
This is a small Starbucks order program
It demonstrate the use of BevelBorder, JCheckBox, &
JRadioButton
*
*/
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
public class Starbucks extends JFrame
{
JButton ok;
JPanel radiopanel;
JPanel checkboxpanel;
JPanel mainpanel;
JRadioButton tall, grande, venti;
JCheckBox ice_green, coffee, orange_juice;
public Starbucks()
{
// Sets title of the window
setTitle("Welcome to Starbucks");
// Sets the size of the window
setSize(300, 400);
// Sets the action if the window is
close
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Creates a button listener
ButtonListener bl = new
ButtonListener();
// Create two panels one for
JRadioButton and one for the main panel
radiopanel = new JPanel();
mainpanel = new JPanel();
// Creates a border for JRadioButton
Border b =
BorderFactory.createTitledBorder("Size");
// Adds the border to the radio panel
radiopanel.setBorder(b);
// Group the radio buttons together
ButtonGroup radiogroup = new
ButtonGroup();
// Add the radio buttons to the panel
and group
tall = new JRadioButton("Tall");
radiopanel.add(tall);
radiogroup.add(tall);
grande = new JRadioButton("Grande");
radiopanel.add(grande);
radiogroup.add(grande);
venti = new JRadioButton("Venti");
radiopanel.add(venti);
radiogroup.add(venti);
// Adds the JRadioButton panel to the
main panel
mainpanel.add(radiopanel);
// Creates the JCheckBox panel
checkboxpanel = new JPanel();
// Creates a border for the check box
panel
Border b2 =
BorderFactory.createTitledBorder("Drinks");
// Adds the border to the check box
checkboxpanel.setBorder(b2);
// Create JCheckbox and add the check
boxes into the panel
ice_green = new JCheckBox("Ice Green
Tea");
checkboxpanel.add(ice_green);
coffee = new JCheckBox("Coffee");
checkboxpanel.add(coffee);
orange_juice = new JCheckBox("Orange
Juice");
checkboxpanel.add(orange_juice);
// Adds the check box panel to the
main panel
mainpanel.add(checkboxpanel);
// Creates a ok button
ok = new JButton("OK");
/*
Creates a
listener for ok button to listen for any events
We pass the
bl into the parameter, bl is created from the very beginning
*
*/
ok.addActionListener(bl);
// Adds the ok button into the main
panel
mainpanel.add(ok);
// Adds the main panel into the
content pane
add(mainpanel);
// Sets the window to be visible
setVisible(true);
}
/*
Inside here, all the events of
JCheckBox and JRadioButton will be handled
*
*/
public class ButtonListener implements ActionListener
{
public void
actionPerformed(ActionEvent e)
{
if(e.getSource()==ok)
{
String drinks = "";
// Determine what is been selected in JCheck Box
if(ice_green.isSelected())
{
drinks += "Ice Green Tea\n";
}
if(coffee.isSelected())
{
drinks += "Coffee\n";
}
if(orange_juice.isSelected())
{
drinks += "Orange Juice\n";
}
// Determine what is been selected in JRadio Button
String msg = "You ordered a ";
if(tall.isSelected())
{
msg += " tall " + drinks;
}
if(grande.isSelected())
{
msg += " grande " + drinks;
}
if(venti.isSelected())
{
msg += " venti " + drinks;
}
// Outputs the order in JOptionPane
JOptionPane.showMessageDialog(null, msg);
/*
Deselected all the check box
Selects tall in radio button
*
*/
ice_green.setSelected(false);
coffee.setSelected(false);
orange_juice.setSelected(false);
tall.setSelected(true);
}
}
}
public static void main(String[] args)
{
new Starbucks();
}
}
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.