I am a Java student and just ended the basic functionality of a little command line card game. The game is a simplified version of Magic-type trading card game. There is no AI, you play against yourself or another human player.
At this point, i am trying to add some GUI to it using MVC, but i'm finding problems adding a MouseListener to a button.
This is a brief explanation of what's going on:
- I have a
Model
class, that extendsObservable
by inheritance of a superclass - A
View
class, that implementsObserver
. - And a
Controller
class that extendsMouseAdapter
Then i put everything together:
....
View view = new View();
Model model = new Model();
model.addObserver( view );
Controller controller = new Controller();
// associate Controller's Model and View objects
controller.addModel(model);
controller.addView(view);
view.addController(controller); // i try to add the MouseListener
....
The addController() method of View is:
public void addController(Controller controller){
this.myButton.addMouseListener( controller )
}
I already checked that addController()
method is being called (println something inside it), but the Listener is not being set for some reason: mouseReleased()
is never called when i click the button.
Any thoughts or any step that i may have overlooked? 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;
}
// All @Overrides
@Override
public void mouseReleased(MouseEvent me) {
System.out.println("oh, it arrived");
}
}