Skip to content

Registering Action Listeners

September 12, 2011

As simple an example as i could come up with to demonstrate how to register a typical button in a Java GUI using Swing components.

1 – create your button something like this:

JButton submit_button = new JButton(“Button Label”); // creates a new JButton object called submit_button.

2 – add to the submit_button, a listening event, like this:

submit_button.addActionListener( new submit_button_click_handler() );

This statement registers an action to take place that is defined by whatever you place in the submit_button_click_handler() method. Notice that the argument to the method addActionListener is a new object.  You must define the class  submit_button_click_handler.  The class must be defined outside of the current class that you are coding.  You could put the class in another file, which is a good practice, but the usual practice for listeners is to define that class in the same class that you are working in.

3 – Define the class like this:

class submit_button_click_handler implements ActionEvent{

}

Notice that the class is implementing an interface called ActionEvent.

4 – The interface ActionEvent requires that you define the action performed like this:

class submit_button_click_handler implements ActionEvent{

public void actionPerformed(ActionEvent e){

//here define what the action is…

}

}

Don’t forget at the top of your class definition to include the required java files, like this:

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

Below is a simple example of how you could implement a button listener in a typical Java GUI.

import javax.swing.*;
import java.awt.event.*;  // Needed for ActionListener

public class Simple_Button {
    
    //constructor - builds the GUI
    Simple_Button(){
        JFrame window = new JFrame();    
        JButton submit_form = new JButton("Click me");
    
        //register the button
        submit_form.addActionListener(new Btn_click_handler());
            
        //show the button
        window.add(submit_form);
        window.pack();
        window.setVisible(true);
    }//end constructor

    public static void main(String args[]){
        new Simple_Button();
    }//end main
}//end Simple_Button class

//button submit_form click handler
class Btn_click_handler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        //actions
        JOptionPane.showMessageDialog(null, "Click me button clicked");
    }//end actionPerformed
}//end class Btn_click_handler
Advertisements

From → Java

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: