Java Observer and Observable not working properly between applications

I have an application with a basic JFrame that contains the default list model. I want that if I change something in these entries, the second running application is automatically updated.

So far, I have a MainController class that implements a listener and overwrites the update method:

public class MainController implements ActionListener, Observer { public void update(Observable o, Object o1) {} } 

and a simple class extending Observable

 public class Comanda extends Observable{} 

My problem is that if I delete one entry from the first application, the second list will not be updated. The program deletes the entry from the text file, but does not update the default list model. Same issue with editing or adding.

I tried adding "reloadList ()" in the update method, but this does not work. Ideas?

+6
source share
2 answers

Did you addObserver on Comanda and add MainController as an Observer ? Also, when a change occurs, do you call setChanged and notifyObservers ?

Looking at the code you posted, I see that you did not bind the Observer and Observable objects together. As I said, you must call addObserver on your Observable and then in your Observable Object, whenever a change occurs, you need to call setChanged , then notifyObservers . Only when notifyObservers is called will the update method of any added Observer be called.

You said in your question that when you delete one entry, the list is not updated, which makes me think that Comanda is probably not the Object that you want Observe . Whatever Object, it does not contain a List records that should be Observable .

See this for more information on the Observer / Observable pattern.

+23
source

What you are trying to do is called "interprocess communication" - sending data from one application to another. There are various ways to do this; a Google search will give you more information. Observable only works in one application.

+2
source

Source: https://habr.com/ru/post/906063/


All Articles