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);
}
});
An ObservableValue is an entity that wraps a value and allows to observe the value for changes. In general this interface should not be implemented directly but one of its sub-interfaces ( ObservableBooleanValue etc.). The value of the ObservableValue can be requested with getValue() .
Class SimpleStringPropertyThis class provides a full implementation of a Property wrapping a String value. Since: JavaFX 2.0 See Also: StringPropertyBase.
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 ;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With