Reputation: 4150
I've a doubt. I don't know where insert each eventlistener in a pattern MVC. Is it right to insert them in a controller or is better insert eventlistener directly in view?And from view eventlistener can call a controller to do something.
Upvotes: 1
Views: 124
Reputation: 1500
Your event listeners should be in the controller. The controller holds an instance of the view and your view has public methods to set the event listeners of the GUI controls(JTextField etc).
Example: You have a view that has a JButton
control called buttonSubmit
and you want to listen for when someone interacts with that button.
View
public void addSubmitButtonListener(ActionListener listener) {
buttonSubmit.addActionListener(listener);
}
Controller
public void run() {
view.addSubmitButtonListener( new SubmitButtonListener() );
}
class SubmitButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent evt) {
// What happens after someone interacts with the button goes in here.
}
}
That SubmitButtonListener
class goes directly into the controller as an inner class.
Upvotes: 1