title: JavaFX Custom Bindings not working
body: <p>I came across this issue on a larger scale application where some custom bindings weren't being updated when the value of the source property changed.</p>
  
  <p>I managed to write a simple class to replicate this issue and I really don't get why this happens. Here's a quick test that replicates the problem:</p>
  
  <pre><code>import javafx.beans.binding.BooleanBinding;
  import javafx.beans.binding.ObjectBinding;
  import javafx.beans.property.BooleanProperty;
  import javafx.beans.property.ObjectProperty;
  import javafx.beans.property.SimpleBooleanProperty;
  import javafx.beans.property.SimpleObjectProperty;
  import javafx.beans.value.ChangeListener;
  import javafx.beans.value.ObservableValue;
  
  public class TestingBindingsFx {
  
    private final ObjectProperty&lt;MyEvent&gt; objectProperty = new SimpleObjectProperty&lt;MyEvent&gt;(this, "objectProperty");
    private final BooleanProperty booleanProperty = new SimpleBooleanProperty(this, "booleanProperty");
  
    private ObjectBinding&lt;MyEvent&gt; bindingObj;
    private BooleanBinding bindingBool;
  
    public TestingBindingsFx(ObjectProperty&lt;String&gt; selection) {
      setupBindings(selection);
    }
  
    private void setupBindings(ObjectProperty&lt;String&gt; selection) {
      bindingObj = createObjectBinding(selection);
      bindingBool = createBooleanBinding(selection);
      objectProperty.bind(bindingObj);
      booleanProperty.bind(bindingBool);
    }
  
    private static ObjectBinding&lt;MyEvent&gt; createObjectBinding(ObjectProperty&lt;String&gt; selection) {
      return new ObjectBinding&lt;MyEvent&gt;() {
        {
          super.bind(selection);
        }
  
        @Override
        protected MyEvent computeValue() {
          System.out.println("createObjectBinding called");
          MyEvent ve = selection.get() == null ? MyEvent.EVENT1
              : MyEvent.EVENT2;
          return ve;
        }
      };
    }
  
    private static BooleanBinding createBooleanBinding(ObjectProperty&lt;String&gt; selection) {
      return new BooleanBinding() {
        {
          super.bind(selection);
        }
  
        @Override
        protected boolean computeValue() {
          System.out.println("createBooleanBinding called");
          return selection.get() == null ? true : false;
        }
      };
    }
  
    public static void main(String[] args) {
      ObjClass objclass = new ObjClass();
      System.out.println("Instantiating TestingBindingsFx...");
      TestingBindingsFx fx = new TestingBindingsFx(objclass.selection);
  
      objclass.selection.addListener(new ChangeListener&lt;String&gt;() {
        @Override
        public void changed(ObservableValue&lt;? extends String&gt; observable, String oldValue, String newValue) {
          System.out.println("changed " + oldValue + "-&gt;" + newValue);
        }
      });
  
      System.out.println("Changing selection property values...");
      objclass.selection.set("Test 1");
      objclass.selection.set("Test 2");
    }
  
    enum MyEvent {
      EVENT1,
      EVENT2;
    }
  
    static class ObjClass {
      public final ObjectProperty&lt;String&gt; selection = new SimpleObjectProperty&lt;String&gt;(this, "selection");
    }
  }
  </code></pre>
  
  <p>So after running this I see:</p>
  
  <blockquote>
  <pre><code>Instantiating TestingBindingsFx...
  createObjectBinding called
  createBooleanBinding called
  Changing selection property values...
  changed null-&gt;Test 1
  changed Test 1-&gt;Test 2
  </code></pre>
  </blockquote>
  
  <p><strong>When I expected to see something like:</strong></p>
  
  <blockquote>
  <pre><code>Instantiating TestingBindingsFx...
  createObjectBinding called
  createBooleanBinding called
  Changing selection property values...
  changed null-&gt;Test 1
  createObjectBinding called
  createBooleanBinding called
  changed Test 1-&gt;Test 2
  createObjectBinding called
  createBooleanBinding called
  </code></pre>
  </blockquote>
  
  <p>The <code>ChangeListener</code> is working as expected (just put it there for verification purposes) and it's getting called every time I change the value of the selection property.</p>
  
  <p>But the custom bindings are never updated after the first time and looking at the code I can't understand why. At first I thought it could be related with weak references, but I even turned the binding objects into class level variables and yet no change.</p>
  
  <p>I feel that I might be missing something crucial here, but after 2 hours of looking at this code I just can't see why.
  On my actual application it's even more strange since one of the custom bindings actually work fine.</p>