More Low Level GUI Components

Review Questions for GUIs:
1. Write Java code that creates a panel and makes the layout for the panel BorderLayout.
2. Write Java code that creates a button. The text on the button should initially read "off", but when it is clicked it should alternate between "on" and "off".
3. Add the button you created in #2 to the south region of the panel you created in #1.
4. Assume that you have a JButton button already created, and that this button's text is the number of times it has been clicked (currently 0). Write Java code that handles clicks on this button.

Text Fields


JTextField textfield = new JTextField(25);
textfield.addActionListener(this); // our class will implement the ActionListener
        // interface


public void actionPerformed(ActionEvent e)
   String text = textField.getText();

Example: Write a program that processes strings entered in a text field. Update a button's text to contain the most recently entered string.


: Write an application which contains a button and a label. The button's text should contain an integer equal to the number of button clicks, and the label should contain a label of the form "Number of button clicks: 3".

Check Boxes


JCheckBox(String label)  // the parameter is the label on the checkbox

JCheckBox(String label, boolean selected) // selected is the initial state of the checkbox. true = selected, false = not selected

boolean isSelected()  // returns state of the checkbox (true = selected)

void setSelected(boolean state) // sets checkbox to a new state


getItemSelectable() // returns the originator of the event

getStateChange() // returns the type of state change (selected or deselected)

class GUIEx implements ItemListener
    public void itemStateChanged(ItemEvent e)
       Object src = e.getItemSelectable();

       if(src == pepperoniButton) { ... // put pepperoni in the list of included

       else if(src == cheeseButton) ...

    if(e.getStateChange() == ItemEvent.DESELECTED) {// remove item from the
     ingredient list}

Example: Write a program with 3 checkboxes. The 3 checkboxes should contain the text "Box 1", "Box 2", and "Box 3". Include a label for each box which contains "Box n is selected" or "Box n is not selected", depending on the state of each box.

Exercise: Write a program that a person might use while ordering a pizza. Include 5 checkboxes for 5 different ingredients: pepperoni, cheese, peppers, pineapple, and onion. Use a label to indicate which toppings will be used for the pizza.

Radio Buttons

To create associated radio buttons, e.g. small, medium and large buttons for pizza size:
1. Construct one object of type ButtonGroup for each group of buttons.

2. Add objects of type JRadioButton to the button group.

3. The button group object deselects the previously selected button when a new button is clicked.

4. The ButtonGroup object forms a relationship between the states of the buttons which are added to it. It does NOT replace the need to put the radio buttons on a panel, content pane, etc. A ButtonGroup object is not a GUI component, so do not try to add it to a container.

5. A radio button generates an action event when clicked, so implement actionPerformed() in the ActionListener interface to handle this event.

JRadioButton smallButton = new JRadioButton("small", false);  // false --> not initially selected
JRadioButton medButton = new JRadioButton("medium", true); // text for button is "medium"
JRadioButton largeButton = new JRadioButton("large", false);

ButtonGroup group = new ButtonGroup();  // this is how we tie the 3 buttons together, so only one option can be chosen

java.util.EventObject (ancestor of ActionEvent class)

Object getSource() // returns the object on which the event initially occurred.

Example: Write a program that uses radio buttons to allow the user to choose a pizza size.  Indicate the current size choice on a label.



JPanel buttonPanel = new JPanel();

Border etched = BorderFactory.createRaisedBevelBorder();

Methods from javax.swing.BorderFactory

static Border createEtchedBorder()  // creates a line border with a 3D effect
static Border createLoweredBevelBorder()  // creates a border that gives the effect of a lowered surface
static Border createRaisedBevelBorder()  // creates a border that gives the effect of a raised surface
static Border createLineBorder(Color color) // creates a simple line border

Border border = BorderFactory.createLineBorder(;

Exercise: Add a border to the panel containing the buttons that represent the pizza's size in our last example.