Learning Java
Custom Search

Previous Next

Action Listener

In this example, we will show how to use the ActionListener interface to respond to the JButton, the JCheckBox, and the JRadioButton components.

In this example, we create a class called CkAction as an extension of the JFrame class. With it, we implement the ActionListener interface which contains only one method, actionPerformed.

After we create each component, the ActionListener is attached to each of them using the following. The "this" keyword refers to the current component, which is the class that implements ActionListener.

    btn.addActionListener(this);
    chk.addActionListener(this);
    choices[i].addActionListener(this);
    colors[i].addActionListener(this);

The choices[i] and colors[i] arrays are each placed in a "for" loop where they are added to 1 of 2 Radio Button groups, get the ActionListener attached to them, and get added to either the panChoice or panColor panel. The phrase "choices.length" produces a value of 3 which is how many members are in the array choices. The value of colors.length is 4.

After the ActionListener is added to each component, the component can be added to the container pane. The panChoice and panColor with their radio buttons are also added to pane.

In the method, actionPerformed(ActionEvent evt), the evt represents the event that happened. We access the source of this action event using the method evt.getSource(). We then determine which component was the source of this action by comparing the source we obtained to each of our components, as shown in the "if...else if..." structure.

In our test, we change the text field depending on which component is clicked.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
public class CkAction extends JFrame implements ActionListener {
    JRadioButton[] choices = new JRadioButton[3];
    JRadioButton[] colors = new JRadioButton[4];
    JCheckBox chk = new JCheckBox("Do you believe in UFO's?");
    JButton btn = new JButton("I'm a button");
    JTextField txt = new JTextField(20);

    public CkAction() {
        super("Testing Events");
        setSize(250, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel pane = new JPanel();
        btn.addActionListener(this);
        chk.addActionListener(this);
        pane.add(btn);
        pane.add(chk);

        JPanel panChoice = new JPanel();
	choices[0] = new JRadioButton("coffee");
	choices[1] = new JRadioButton("tea");
    	choices[2] = new JRadioButton("milk");
	ButtonGroup radiobtns1 = new ButtonGroup();

	for (int i = 0; i < choices.length; i++) {
	   radiobtns1.add(choices[i]);
	   choices[i].addActionListener(this);
	   panChoice.add(choices[i]);
	}

        JPanel panColor = new JPanel();
	colors[0] = new JRadioButton("red");
	colors[1] = new JRadioButton("green");
    	colors[2] = new JRadioButton("blue");
    	colors[3] = new JRadioButton("yellow");
	ButtonGroup radiobtns2 = new ButtonGroup();

	for (int i = 0; i < colors.length; i++) {
	   radiobtns2.add(colors[i]);
	   colors[i].addActionListener(this);
	   panColor.add(colors[i]);
	}

        pane.add(panChoice);
        pane.add(panColor);
        pane.add(txt);

        setContentPane(pane);
	setVisible(true);
    }

    public static void main(String[] arguments) {
        CkAction test = new CkAction();
    }

    public void actionPerformed(ActionEvent evt) {
        Object source = evt.getSource();
        if (source == btn) {
	    txt.setText("Button Action");
	} else if (source == chk) {
	    txt.setText("UFO hmmmm");
	} else if (source == choices[0]) {
	    txt.setText("Coffee chosen");
	} else if (source == choices[1]) {
	    txt.setText("Tea chosen");
	} else if (source == choices[2]) {
	    txt.setText("Milk chosen");
	} else if (source == colors[0]) {
	    txt.setBackground(Color.red);
	} else if (source == colors[1]) {
	    txt.setBackground(Color.green);
	} else if (source == colors[2]) {
	    txt.setBackground(Color.blue);
	} else if (source == colors[3]) {
	    txt.setBackground(Color.yellow);
	} 
    }
}

Previous Next

Steps In Learning

Contact us

Copyright © 2008      N. Nelson      All Rights Reserved