Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top