Implementing an ObservableValue

user3640448 picture user3640448 · Oct 11, 2014 · Viewed 20.3k times · Source

I have this object:

public class Oggetto{
    private int value;
    private boolean valid;

    public Oggetto(int value, boolean valid) {
        this.value = value;
        this.valid = valid;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public boolean isValid() {
        return valid;
    }

    public void setValid(boolean valid) {
        this.valid = valid;
    }    
}

and I would like implement an Observable object that fires events when something inside changes Here the observable object:

public class OggettoOsservabile implements ObservableValue<Oggetto>{

    private Oggetto value;

    OggettoOsservabile(int i, boolean b) {
        this.value=new Oggetto(i, b);
    }
    @Override
    public void addListener(ChangeListener<? super Oggetto> listener) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    @Override
    public void removeListener(ChangeListener<? super Oggetto> listener) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    @Override
    public Oggetto getValue() {
        return value;
    }
    @Override
    public void addListener(InvalidationListener listener) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    @Override
    public void removeListener(InvalidationListener listener) {
        throw new UnsupportedOperationException("Not supported yet."); 
    }  
}

i dont know how to proceed in order to detect a change in the class "Oggetto" and send a notification to the registeres listener.

OggettoOsservabile oggetto = new OggettoOsservabile(1, false);              
oggetto.addListener(new ChangeListener<Oggetto>() {
     public void changed(ObservableValue<? extends Oggetto> observable, Oggetto oldValue, Oggetto newValue) {
           System.out.println("changed " + oldValue + "->" + newValue);   
     }
});

Answer

James_D picture James_D · Oct 11, 2014

Implement your Oggetto class using standard JavaFX Properties:

import javafx.beans.property.BooleanProperty ;
import javafx.beans.property.IntegerProperty ;
import javafx.beans.property.SimpleBooleanProperty ;
import javafx.beans.property.SimpleIntegerProperty ;

public class Oggetto {

    private final IntegerProperty value = new SimpleIntegerProperty() ;

    public final IntegerProperty valueProperty() {
        return value ;
    }

    public final int getValue() {
        return value.get();
    }

    public final void setValue(int value) {
        this.value.set(value);
    }

    private final BooleanProperty valid = new SimpleBooleanProperty();

    public final BooleanProperty validProperty() {
        return valid ;
    }

    public final boolean isValid() {
        return valid.get();
    }

    public final void setValid(boolean valid) {
        this.valid.set(valid);
    }

    public Oggetto(int value, boolean valid) {
        setValue(value);
        setValid(valid);
    }
}

This may be all you need, as you can just observe the individual properties. But if you want a class that notifies invalidation listeners if either property changes, you can extend ObjectBinding:

import javafx.beans.binding.ObjectBinding ;

public class OggettoObservable extends ObjectBinding {

    private final Oggetto value ;

    public OggettoObservable(int value, boolean valid) {
        this.value = new Oggetto(value, valid);
        bind(this.value.valueProperty(), this.value.validProperty());
    }

    @Override
    public Oggetto computeValue() {
        return value ;
    }
}