how to Notify listeners if the Observable class is deprecated, there is a listener. I need to notify all listeners that there has been a language change. The listener is not working, because deprecated class Observable if version high 9.
interface with a single update method
public interface ListenObservable {
void update(ListenObservable o, Object arg);
}
implementation of a class, almost a copy of Observable
public class ListenObservableImpl {
private boolean changed = false;
private ArrayList<ListenObservableImpl> obs;
/**
* Construct an Observable with zero Observers.
*/
public ListenObservableImpl() {
obs = new ArrayList<>();
}
public void notifyObservers() {
notifyObservers(null);
}
public void notifyObservers(Object arg) {
Object[] arrLocal;
synchronized (this) {
if (!changed)
return;
arrLocal = obs.toArray();
clearChanged();
}
for (int i = arrLocal.length - 1; i >= 0; i--) {
((ListenObservableImpl) arrLocal[i]).update(this, arg));
}
}
protected synchronized void clearChanged() {
changed = false;
}
protected synchronized void setChanged() {
changed = true;
}
public synchronized void addObserver(ListenObservableImpl o) {
if (o == null)
throw new NullPointerException();
if (!obs.contains(o)) {
obs.addElement(o);
}
}
public synchronized boolean hasChanged() {
return changed;
}
//
// private void setValid(Boolean valid) {
// this.valid = valid;
// }
private void update(ListenObservable o, Object arg) {
}
}
...
@FXML
private ComboBox changeLocaleBox;
...
// listens to language change
changeLocaleBox.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Language selectedLang = (Language) changeLocaleBox.getSelectionModel().getSelectedItem();
ManagerLocale.setCurrentLang(selectedLang);
// I need to notify all listeners that there has been a language change.
setChanged(); //Deprecated class Observable
notifyObservers(selectedLang); //Deprecated class Observable
}
});