March 22, 2002
n "It Might Be Efficient, But It Ain't OO," I presented an alternative way to implement the java.awt.ActionListener interface. In my opinion, creating one large actionPerformed() method and simply switching between the various action commands does not represent the best approach. I said such an approach
doesn't follow good object-oriented (OO) design.
Such statements always cause controversy, and, needless to say, my comments did! In fact, my answer generated quite a bit of reader feedback. In this Java Q&A, I present concrete examples I hope will justify my position and help you implement this practice in your programs. As an added bonus, my approach applies to any interface implementation you might write.
Let's start with a simple GUI (graphical user interface).

Figure 1. A simple GUI
Figure 1's GUI comprises a panel containing three buttons and a label. When you press a button, the GUI writes a message to the label indicating which button you pressed.
To display the panel, I embedded it within a frame. However, for the purposes of today's Java Q&A, I'll simply focus on the panel code. (Please see Resources for the full source code.)
Following the discussion in the original Java Q&A, you can implement the buttons' listeners in two ways. First, the panel can implement the ActionListener interface directly, or, second, you can create three listener classes whose instances listen to the buttons directly. Let's
look at the first choice in which the panel implements one large listener.
The following class shows how to wire the buttons directly to the panel:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* The ActionListenerGUI is an example of a simple GUI that directly implements
* the ActionListener Interface.
* @author Tony Sintes
*/
public class ActionListenerPanel extends JPanel implements ActionListener {
public ActionListenerPanel() {
setup();
}
private void setup() {
setLayout( new BorderLayout() );
JButton button1 = new JButton( BUTTON_1 );
button1.setActionCommand( BUTTON_1_ACTION );
button1.addActionListener( this );
JButton button2 = new JButton( BUTTON_2 );
button2.setActionCommand( BUTTON_2_ACTION );
button2.addActionListener( this );
JButton button3 = new JButton( BUTTON_3 );
button3.setActionCommand( BUTTON_3_ACTION );
button3.addActionListener( this );
JPanel buttons = new JPanel();
buttons.add( button1 );
buttons.add( button2 );
buttons.add( button3 );
add( display, BorderLayout.NORTH );
add( buttons, BorderLayout.SOUTH );
}
public void actionPerformed( ActionEvent actionEvent ) {
String actionName = actionEvent.getActionCommand().trim();
if( actionName.equals( BUTTON_1_ACTION ) ) {
display.setText( BUTTON_1 );
}
else if( actionName.equals( BUTTON_2_ACTION ) ) {
display.setText( BUTTON_2 );
}
else if( actionName.equals( BUTTON_3_ACTION ) ) {
display.setText( BUTTON_3 );
}
}
private JLabel display = new JLabel("Press a button");
private static String BUTTON_1 = "Button 1";
private static String BUTTON_2 = "Button 2";
private static String BUTTON_3 = "Button 3";
private static String BUTTON_1_ACTION = "B1";
private static String BUTTON_2_ACTION = "B2";
private static String BUTTON_3_ACTION = "B3";
}
In the code above, notice the setup() and actionPerformed() methods. In setup(), I create three buttons. Each button creation takes the following form:
Logo design service |By Kristen Stewart on October 13, 2009, 5:33 amLogo design service | stationary design | web design
Reply | Read entire comment
what do u mean Public void actionPerformed(ActionEvent ae) By Venky on July 6, 2009, 10:10 amThe Statment Public void actionPerformed(ActionEvent ae) means that The class is utilizing ActionListener interface so that to get specific Action when u are going...
Reply | Read entire comment
hmmBy Limited_Atonement on February 23, 2009, 12:49 pmIt looks a lot like the declaration of a routine that will return nothing (void) and takes an event argument. Why do you ask? This among the earliest question...
Reply | Read entire comment
Just wondering if you didBy msk_1980 on October 28, 2008, 1:59 amJust wondering if you did take any pain to read the API. Read the API and still if you cannot understand, you can ask someone to help you understand.
Reply | Read entire comment
need helpBy Anonymous on October 22, 2008, 2:04 pmExplain the purpose of the following statement. public void actionPerformed(ActionEvent event)
Reply | Read entire comment
View all comments