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:
hiBy Anonymous on October 27, 2009, 2:36 amHey everyone ---------------------- gaming game videos game news game reviews game guides game pictures game help games gamer blogs game forums game podcasts
Reply | Read entire comment
helpBy need on October 23, 2009, 9:01 amThank you very much for this information. Good post thanks for sharing. I like this site ;) ----------- promosyon promosyon ürünleri seo seo dan??manl??? penis...
Reply | Read entire comment
custom logo | brochureBy Kristen Stewart on October 13, 2009, 5:38 amcustom logo | brochure design
Reply | Read entire comment
Logo design service |By Kristen Stewart on October 13, 2009, 5:33 amLogo design service | stationary design | web design
Reply | Read entire comment
???? isBy TUTU on September 8, 2009, 6:17 am???? is wonderful. ????????? is wonderful. ??? is wonderful. ?????? is wonderful. ???? is wonderful. ????????? is wonderful. ???? is wonderful. ?? is wonderful. ????? is...
Reply | Read entire comment
View all comments