Java observer and observable do not work properly between applications
I have an application that contains the main JFrame of the default list model I hope that if I modify the contents of these records, the second running application will be updated automatically
So far, I have a maincontroller class that implements the listener and overrides 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 a record from the first application, the second list will not be updated The program is deleting records from the text file without updating the default list model The same problem exists with editing or adding
I tried to add "reloadlist()" to the update method, but it didn't work Ideas?
Solution
Did you call addobserver on Comanda and add maincontroller as observer? Also, when you make a change, are you calling setchanged and notifyobservers?
Looking at your published code, I can see that you do not connect observer and observable objects together As I said, you have to call addObserver on your Observable object, and then in your Observable object, whenever you make changes, you need to call setChanged and call notifyObservers.. The update method of any observer that has been added is called only when notifyobservers is called
You said in your question that when you delete a record, the list will not be updated, which makes me think Comanda may not be the object you want to observe The object that holds the record list should be observable
For more information about observer / observable mode, see this