I am a Java student and have just completed the basic functionality of a small command line card game. The game is a simplified version of the Magic-type card game. No AI, you play against yourself or another person.
At this point, I am trying to add some GUI to it using MVC, but I found problems with adding MouseListener to the button .
This is a brief explanation of what is happening:
- I have a
Model class that extends Observable by inheriting a superclass - A
View class that implements Observer . - And a
Controller class that extends MouseAdapter
Then I put it all together:
.... View view = new View(); Model model = new Model(); model.addObserver( view ); Controller controller = new Controller(); // associate Controller Model and View objects controller.addModel(model); controller.addView(view); view.addController(controller); // i try to add the MouseListener ....
AddController () method for View:
public void addController(Controller controller){ this.myButton.addMouseListener( controller ) }
I already checked that the addController() method is addController() called (println is something inside it), but the Listener for some reason is not being set: mouseReleased() never called when I click the button.
Any thoughts or any step I could miss? Appreciate.
Edit (controller code):
public class Controller extends MouseAdapter { Model model; View view; public void addModel(Model m){ this.model = m; } public void addView(View ui){ this.view = ui; }
source share