Search code examples
javamodel-view-controllerjframeobserver-patternobservable

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.


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.