0
votes

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.

2
calling notifyObservers() does not notify listeners unless setChanged has been called. I take it the notifyObservers is hooked up as an ActionListener of sorts? - pimaster
Thank you :) that solved my problem :) been hours now, never done anything with observers now. - Shane van Wyk

2 Answers

2
votes

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.

0
votes

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.