title: javafx: Bindings not working as expected
body: <p>I am trying to have a property <code>total</code> which is obtained by multiplying two properties together, namely <code>currentPrice</code> and <code>volumeHeld</code>, where <code>currentPrice</code> is actually obtained by downloading google finance stock price every 10 secs. And it automatically updates every 10 seconds. </p>
  
  <p>Now the <code>getCurrentPrice()</code> is initialized at <code>0</code>, as shown in the code. 10 seconds later, it picks up a new value and this all works fine. </p>
  
  <p>But in the binding method below, the <code>total</code> is not automatically updated when the <code>currentPrice</code> property has changed. </p>
  
  <pre><code>totalBinding = Bindings.createDoubleBinding(() -&gt; {
          System.out.println("current price: " + getCurrentPrice() + "vol held: " + getVolumeHeld());
          return getCurrentPrice() * getVolumeHeld();
      });
  
     total.bind(totalBinding);
  </code></pre>
  
  <p><strong>Question:</strong> I discovered that within the <code>createDoubleBinding</code> statement above, the <code>getCurrentPrice()</code> has a value of 0 (as mentioned above) and when its value is changed, the change is NOT propagated in the <code>total</code> property. By that I mean the <code>total</code> property is not able to pick up the new value from <code>getCurrentPrice()</code> even when the current price has changed. </p>
  
  <p>So the problem is two-fold but I am guessing the solutions for both of my questions below will be similar if not exactly the same:</p>
  
  <ol>
  <li><p>How can I fix the problem mentioned above?</p></li>
  <li><p>Later on, I will be binding this <code>total</code> property to another property to work out the total of the <code>total</code> property for all <code>Trade</code> objects). This fails miserably and it is always equal to 0. <strong>This method is written in a different class, i.e. not in the Trade class.</strong></p></li>
  </ol>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Code shown below:</p>
  
  <pre><code>class SummaryofTrade{
      ...     
      sumOfTotals = new ReadOnlyDoubleWrapper();
      sumOfTotalsBinding = Bindings.createDoubleBinding(() -&gt; {
          double sum = 0;
          for(Trade t : this.observableListOfTrades){
              sum += t.getTotal();
          }
          return sum;         
      }, total);     // I cannot put "total" as a second parameter, as it is a property that resides in the Trade class , not this class.
      sumOfTotals.bind(sumOfTotalsBinding);
      ...
  }
  </code></pre>
  
  <p>The error log message: </p>
  
  <pre><code>Caused by: java.lang.Error: Unresolved compilation problem: 
      total cannot be resolved to a variable
  </code></pre>
  
  <p>Please note that the <code>sumOfTotalsBinding</code> and <code>sumOfTotals</code> live in another class. </p>
  
  <p>Code for Trade object below: </p>
  
  <pre><code>class Trade{
        ...
        private final ReadOnlyDoubleWrapper total;
        private final ReadOnlyDoubleWrapper currentPrice;
        private DoubleProperty volumeHeld;
        public DoubleBinding totalBinding;
  
  
  
        private final ScheduledService&lt;Number&gt; priceService = new ScheduledService&lt;Number&gt;() {
          @Override
          public Task&lt;Number&gt; createTask(){
              return new Task&lt;Number&gt;() {
                  @Override
                  public Number call() throws InterruptedException, IOException {
                      return getCurrentPriceFromGoogle();
                  }
              };
          }
         };
  
      public Trade(){
         ...
         priceService.setPeriod(Duration.seconds(10));
          priceService.setOnFailed(e -&gt; priceService.getException().printStackTrace());
          this.currentPrice   = new ReadOnlyDoubleWrapper(0);
          this.currentPrice.bind(priceService.lastValueProperty());
          startMonitoring();
          this.total          = new ReadOnlyDoubleWrapper();
          DoubleBinding totalBinding = Bindings.createDoubleBinding(() -&gt;
            getCurrentPrice() * getVolumeHeld(),
            currentPriceProperty(), volumeHeldProperty());                
         total.bind(totalBinding);
       }
  
  
          // volume held
      public double getVolumeHeld(){
          return this.volumeHeld.get();
      }
  
      public DoubleProperty volumeHeldProperty(){
          return this.volumeHeld;
      }
  
      public void setVolumeHeld(double volumeHeld){
          this.volumeHeld.set(volumeHeld);
      }
  
          // multi-threading
      public final void startMonitoring() {
           priceService.restart();
      }
  
      public final void stopMonitoring() {
          priceService.cancel();
      }
  
          public ReadOnlyDoubleProperty currentPriceProperty(){
           return this.currentPrice.getReadOnlyProperty();
      }
  
      public final double getCurrentPrice(){
          return currentPriceProperty().get();
      }
  
          // total
      public final Double getTotal(){
          return totalProperty().getValue();
      }
  
      public ReadOnlyDoubleProperty totalProperty(){
          return this.total;
      }
  }
  </code></pre>
  
  <p><strong>UPDATE 9/15/2015:</strong></p>
  
  <p>I am trying to elaborate my problem in a logical way here. Let me know if this does not make sense. Thanks.</p>
  
  <p>First, in the <code>Trade class</code> above (please note the code above has been updated and <strong>specified the property dependency</strong>), each Trade object contains a <code>total</code> property, which is the product of <code>currentPrice</code> and <code>VolumeHeld</code>. If the user manually edit the values of current price and volume held. The <code>total</code> property will be updated automatically.  </p>
  
  <p>Now, I have an ObservableList of Trade objects, each of them has a <code>total</code> property. My goal is to sum up the the <code>total</code> property of each Trade object in the observable list and bind the sum to a variable called <code>sumOfTotals</code>. This is done in a class called <code>SummaryOfTrade</code>. And whenever the <code>total</code> property of any one of the Trades in the Observable list changes, the <code>sumOfTotals</code> property should also change automatically. </p>
  
  <pre><code>class SummaryofTrade{
      ...     
      // within constructor, we have
      sumOfTotals = new ReadOnlyDoubleWrapper();
      sumOfTotalsBinding = Bindings.createDoubleBinding(() -&gt; {
          double sum = 0;
          for(Trade t : this.observableListOfTrades){
              sum += t.getTotal();
          }
          return sum;         
      }, totalProperty());    
      sumOfTotals.bind(sumOfTotalsBinding);
      ...
  }
  </code></pre>
  
  <p>This is where the problem comes in. Eclipse is saying that it does not recognise the Trade object's property,<code>totalProperty</code>. Error message shown below. </p>
  
  <p>The error log message: </p>
  
  <pre><code>Caused by: java.lang.Error: Unresolved compilation problem: 
      The method totalProperty() is undefined for the type SummaryOfTrade
  </code></pre>
  
  <p>I have specified the property dependency already yet Eclipse is throwing an error. How should I resolve this?</p>