Shane van Wyk
Shane van Wyk

Reputation: 1900

Java View to Controller Observer

I need help. I am struggling to get my Observers working in java. Can someone explain to me using MODEL-VIEW-CONTROLLER Architecture how to create and observer from View To Controller.

This is because if i press a button on the view the action event has to call notify the controller of that button being pressed.

For that I'm implementing observers to minimize class coupling.

I have a class Controller, View (Swing using JFrame), and an Application Class that holds the main method.

I tried implementing it so that Controller implements Observer and the View extends Observable.

After triggering the event of clicking the button all code except the notifyObservers("OBJECT") gets called. It disappears somewhere in the java library.

Any Help Will be much appreciated.

Upvotes: 0

Views: 1357

Answers (2)

Ray Tayek
Ray Tayek

Reputation: 10003

the model should extend observable and the view should implement observer (you want the view to depend on the model). you will need to call setChanged to after you change the state of the model to force the observers to be notified.

Upvotes: 2

Andreas Dolk
Andreas Dolk

Reputation: 114757

Double check, that your controller is really observing/listening to the (correct) button instance. Use a debugger and set some breakpoints to check whether notifyObservers is called and who is receiving the notification.

Upvotes: 0

Related Questions