I have an application with a main JFrame that contains a default list model. I want that if I modify something on these records, the second running application is automatically updated.
So far I have a MainController class which implements the listener and overwrites the update method:
public class MainController implements ActionListener, Observer {
public void update(Observable o, Object o1) {}
}
and a simple class that extends Observable
public class Comanda extends Observable{}
My problem is that if I delete one record from the first application, the second list doesn’t update. The program is deleting the record from the text file but is not updating the default list model. Same problem with edit or add.
I tried adding “reloadList()” in the update method, but that doesn’t work. Ideas?
Have you called
addObserveronComandaand added theMainControlleras anObserver? Also, when the change occurs are you callingsetChangedandnotifyObservers?Looking at the code you have posted I can see that you have not wired the
ObserverandObservableObjects together. As I said, you have to calladdObserveron yourObservableobject, then within yourObservableObject, whenever a change is made you need to callsetChangedthennotifyObservers. Only whennotifyObserversis called will theupdatemethod of anyObservers that have been added be called.You said in your question that when you delete one record the list doesn’t update, which makes me think that
Comandais probably not theObjectthat you want toObserve. Whichever Object it is that holds theListof records is the one that should be theObservable.Have a look at this for some more information on the Observer/Observable pattern.