Search code examples
javaobserver-pattern

One program, 2 windows. How to notify #2 from #1 of change using observer?


Edit: What I really want is: How to activate update from the second window, but from the first. I have added both update(s).

I have 2 windows, each windows has an observer. When I update anything within that window with an observer, it works!

However I'd like to also update the other window. I think my observer isn't right, no idea how to change it though

public class ObserverCtrl extends Observable {

    public ObserverCtrl(Secretariat window, ) {

        this.addObserver(window);
    }

    public ObserverCtrl(Comisie window) {

        this.addObserver(window);
    }



    public void refresh () {
        this.setChanged();
        this.notifyObservers();
    }

}

Main file

Secretariat window = new Secretariat();
Comisie window2 = new Comisie();

Each JFrame (window) has

private ObserverCtrl observer;
this.observer = new ObserverCtrl(this)

First window, Secretariat

@Override
public void update(Observable arg0, Object arg1) {
    candidat_TextField.setText(null);
    sectie_TextField.setText(null);
    codSectie_TextField.setText(null);
    confirmare_TextArea.setText(null);
    listModelCandidati.clear();
    listModelSectii.clear();
    loadSectiiListModel();
    loadCandidatiListModel();
}

Second Window, Comisie

@Override
public void update(Observable arg0, Object arg1) {
    this.loadSectiiListModel();

}

I have an event click button in Secretariat. It activates update() for itself, I'd like it to also activate update for Comisie

How should I have implemented thing so that informing window #2 from #1 via the Observer would have been possible ?


Solution

  • I have made another class that extends Observable, used that to notify Window #2