In the code snippet below value1 is bidirectionally bound to value2 which is then bidirectionally bound to value3. An Invalidation listener is added to value3. When value1 is set for the first time the invalidation listener is called. The invalidation listener is not called for subsequent changes to value1. It is interesting to note that when value3.get() is called from inside the invalidation listener it seems to fix the problem.
//---------------------------------------- CODE --------------------------------------------
package fxtest;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class InvalidationListenerNotCalled {
private static int count = 0;
public static void main(final String[] args) {
final StringProperty value1 = new SimpleStringProperty();
final StringProperty value2 = new SimpleStringProperty();
final StringProperty value3 = new SimpleStringProperty();
value1.bindBidirectional(value2);
value2.bindBidirectional(value3);
value3.addListener(new InvalidationListener() {
@Override
public void invalidated(final Observable observable) {
// Insert the following line to receive invalidation calls
//value3.get();
count++;
}
});
System.out.println("count --> Expected: 0 - Actual: " + count);
value1.setValue("123");
System.out.println("count --> Expected: 1 - Actual: " + count);
value1.setValue("456");
System.out.println("count --> Expected: 2 - Actual: " + count);
}
}
//---------------------------------------- CODE --------------------------------------------
package fxtest;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class InvalidationListenerNotCalled {
private static int count = 0;
public static void main(final String[] args) {
final StringProperty value1 = new SimpleStringProperty();
final StringProperty value2 = new SimpleStringProperty();
final StringProperty value3 = new SimpleStringProperty();
value1.bindBidirectional(value2);
value2.bindBidirectional(value3);
value3.addListener(new InvalidationListener() {
@Override
public void invalidated(final Observable observable) {
// Insert the following line to receive invalidation calls
//value3.get();
count++;
}
});
System.out.println("count --> Expected: 0 - Actual: " + count);
value1.setValue("123");
System.out.println("count --> Expected: 1 - Actual: " + count);
value1.setValue("456");
System.out.println("count --> Expected: 2 - Actual: " + count);
}
}