Let's assume I have an entity class Foo which contains some fields, getters, setters and constructor. For example:
public class Foo {
private Integer a = 0;
private Integer b = 0;
public Foo() {
}
public Integer getA() {
return a;
}
public void setA(Integer a) {
this.a = a;
}
public Integer getB() {
return b;
}
public void setB(Integer b) {
this.b = b;
}
}
Then i want to know when a or b changes. I know that there is an ObjectProperty in javaFX. So i'm creating Object Property:
ObjectProperty<Foo> fooProperty = new SimpleObjectProperty<>(new Foo());
Then in order to know about changes of a and b fields, I add ChangeListener:
fooProperty.addListener((observable, oldValue, newValue) -> {
System.out.println("Property changed!");
});
Then experiments:
fooProperty.set(new Foo());
working well, but next line:
fooProperty.get().setA(10)
does not invoke listener. The reason for that is clear. Last line actually doesn't change the Foo object (reference remains the same).
I have some possible solutions. But all of them are not elegant enough. For example changing each field as a property and adding the same listener for each variable.
So, the question is. What is the way to add listener for changes of the fields of custom object?
a
andb
asIntegerProperty
s and using a fixedFoo
instance? Or am I misunderstanding the question? – Gastrocnemius