开发者

Java View to Controller Observer

开发者 https://www.devze.com 2023-03-31 04:48 出处:网络
I need help. I am struggling to get my Observers worki开发者_运维知识库ng in java. Can someone explain to me using MODEL-VIEW-CONTROLLER Architecture how to create and observer from View To Controller

I need help. I am struggling to get my Observers worki开发者_运维知识库ng 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消