3

I have been told by my teacher that having a separate class for the actionListener is best. In the separate class, should I just make one actionListener method eg.

public void actionPerformed(ActionEvent e) {
    if(e.getSource() == myButton) {
        //do something
    }
    else if(e.getSource() == myComboBox) {
        //do something
    }
}

or should I make many? I have a feeling I should make many but I have no idea how to go about it. What would the different parameters be? I will be making an instance of this class in my View class where the button and combobox is. (this would be the Controller class as i'm trying to do MVC)

For the button I would like for the background to change colour when the mouse hovers over it and then of course do something when clicked. The combobox simply needs to return whatever option was selected as String.

mawburn
  • 2,232
  • 4
  • 29
  • 48
cornchips007
  • 43
  • 1
  • 5
  • I have been told the best solution is to make a seperate class for every ActionListener I need. This is good for modularity and decoupling. – cornchips007 May 28 '14 at 05:35
  • Just letting everyone know, I got confused because I thought I was supposed to make one class with many different methods using one ActionListener, without that long if-else if-else thing. I thought every time you make a new method in the one class you'd be making another ActionListener which is NOT the case. You actually have to make a new class which implements ActionListener for it to be a whole new listener. Sorry!! – cornchips007 May 28 '14 at 05:57

2 Answers2

2

When you make a separate class for the action listener, you can reuse the action event you made.

I will give you an example for that

This class have an action listener

class  AListener implements ActionListener
{
    JTextField tf;
    AListener(JTextField tf)
    {
        this.tf=tf;
    }

    public void actionPerformed(ActionEvent e)
    {
        if((e.getActionCommand()).equals("Button1"))
            tf.setText("Button1 clicked");
        if((e.getActionCommand()).equals("Button2"))
            tf.setText("Button2 clicked");
        if((e.getActionCommand()).equals("Button3"))
            tf.setText("Button3 clicked");
    }
} 

And this is the GUI class, which has 3 buttons and one text field and each button print something different in the text field, so as you can see, it is not logical to make 3 different action listener methods in the GUI class.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ActionDemo extends JFrame 
{
    public ActionDemo() 
    {
        setLayout(new FlowLayout());

        // Add buttons to the frame
        JButton b1=new JButton("Button1");
        JButton b2=new JButton("Button2");
        JButton b3=new JButton("Button3");
        JTextField tf=new JTextField(10);

        AListener listen=new  AListener(tf);
        b1.addActionListener(listen);
        b2.addActionListener(listen);
        b3.addActionListener(listen);

        add(b1);add(b2);add(b3);add(tf);
  }

  public static void main(String[] args) 
  {
      ActionDemo frame = new ActionDemo();
      frame.setTitle("Action Demo");
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(500, 100);
      frame.setVisible(true);
  }
}

So now, every time a button clicked, one action listener method only will be called. Inside this method, I will check which button called me.

Ahmed Hamdy
  • 2,547
  • 1
  • 17
  • 23
2

I prefer to write a separate action listener for each controller action. It keeps things simpler for my simple mind.

My rule of thumb is, for very short (1 - 3 lines) action listeners, I'll make them inline anonymous classes.

For medium size action listeners (1 - 3 methods), I'll make them an inner class, especially if the action listener needs 3 or more fields from the outer main class.

For large action listeners, I'll write a separate class.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • Writing a seperate class for each ActioinListener does definitely increase modularity, and decoupling. My teacher has recommended keeping each ActionListener in seperate classes to prepare for big projects. – cornchips007 May 28 '14 at 05:32
  • 2
    You're welcome. Check out my [Java Swing File Browser](http://java-articles.info/articles/?p=637) article when you have a chance. The number of action listeners in a GUI can get high. – Gilbert Le Blanc May 28 '14 at 15:54