Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

With listener design, OO matters

Find out the best way to design your listeners

  • Print
  • Feedback

March 22, 2002

In "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.)

Implementation

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.

Wire the buttons directly to the panel

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:

  • Print
  • Feedback

Resources