Java: Button Press – Action Event

How to use the ActionEvent for buttons, list selections, etc., is explained in this tutorial.


The ActionEvent is a class called when an action is performed on items in your GUI. As a simple example, we will handle the event on a button press. The code will only output text, but you can add any code you want.

CODE:

import java.io.*;
import java.awt.*;
import java.util.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class AE extends JFrame{
static final int WIDTH = 500;
static final int HEIGHT = 100;

JButton b;
B button;

public class display extends JPanel{
public display(){
Container container;
GridBagLayout layout = new GridBagLayout();
GridBagConstraints layoutConstraints = new GridBagConstraints();
getContentPane().setLayout(layout);
container = getContentPane();

b = new JButton("Press!");
layoutConstraints.gridx = 0; layoutConstraints.gridy = 0;
layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.insets = new Insets(1,1,1,1);
layoutConstraints.anchor = GridBagConstraints.CENTER;
layoutConstraints.weightx = 1.0; layoutConstraints.weighty = 1.0;
layout.setConstraints(b, layoutConstraints);
container.add(b);
}
}

public AE(){
JFrame j = new JFrame();
j.getContentPane().add(new display());
button = new B();
b.addActionListener(button);

}

private class B implements ActionListener{
public void actionPerformed(ActionEvent e){
System.out.println("Button Pressed");
}
}

public static void main(String args[]){
AE a = new AE();
a.setSize(WIDTH,HEIGHT);
a.setVisible(true);
}
}

To reduce the creation of multiple instances of the press method, we create a class for it and assign it to button. Thus, this assignment is done once, and then the single copy is called each time the button is pressed.

We assign the action code with b.addActionListener(button);.

The class may have any name you like, but the method name public void actionPerformed(ActionEvent e) is non-negotiable.
All code within this method will be run when this particular action occurs. There are many action events for each GUI object, such as document listeners for TextAreas, etc.
Check the java documentation for a full list. I will post examples of a few more common ones as well.

Questions/Comments: william_a_wilson@hotmail.com
-William. § (marvin_gohan)

The Conversation

Follow the reactions below and share your own thoughts.

  • willy

    ..do you have any sample program with events…(awt,swing,applet)

  • Confused

    I found the names chosen here confusing. You have an instance of a JButton named “b”, then you have a class name “B”, which is the action listener. Then, when you instantiate the handler, you call it “button”, when it’s not a button. See below:

    JButton b;
    B button;

  • Alicia_mcc

    Im having lots and lots of trouble trying to create a “half-@**ed” mp3 player, currently just trying to work it with a button press that will start a song, using javazoom.jl.player.*; i cannot figure it out for the life of me, ive practically read every tidbit of code in the jlayer help files for the javazoom player, im on the verge of ripping my hair out!! any way you could help me!?!?! Would me MUCH appreciated!!! shout me out at alicia_mcc@live.ca