3

I am writing program with GUI using MVC design pattern. I have a question concerning using ActionListeners.

According to the MVC pattern, all of the ActionListeners should be included into Controller. Normally, as I believe they will be implemented into inner classes like.

However in case of many buttons etc. is it a good idea to move those inner classes to separate files in the same package? I know that they will not be inner classes any more, so is it a good design? And as I would need to use local variable from the Controller class can I simply set them a default access?

Controller class:

public class Controller{ 

    GoogleMaps gMaps = null; // model
    GUI gui = null;          // view 

    public Controller(GoogleMaps gMaps, GUI gui) {
        super();
        this.gMaps = gMaps;
        this.gui = gui; 

        this.gui.addButtonDownListener(new ButtonDownListener(this));
    }
}

ButtonDownLister class:

class ButtonDownListener implements ActionListener{

    private BudgetController buttonDownListener;

    public ButtonDownListener(BudgetController buttonDownListener) {
        super();
        this.buttonDownListener = buttonDownListener;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // some ActionEvent handler
    }
}
pezetem
  • 2,503
  • 2
  • 20
  • 38
  • Why you pass the controller as parameter in the actionListener? You want it like a `Mediator`? – nachokk Dec 16 '13 at 20:45
  • I need to use manipulate model (gMaps) in the actionListener, without passing the controller I cannot get access to it – pezetem Dec 16 '13 at 20:47

1 Answers1

1

Might not be perfect MVC, but you can still have inner class ActionListeners that just make calls to the controller that actually carries out the work.

So the // some ActionEvent handler is still completed in the Controller.

jzd
  • 23,473
  • 9
  • 54
  • 76
  • Conversely, [`Action`](http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html) is a convenient way to encapsulate functionality and export it to the controller. – trashgod Dec 17 '13 at 02:01