title: JavaFX: How to bind two values?
body: <p>I'm new guy here :)</p>
  
  <p>I have a small problem which concerns binding in JavaFX. I have created Task which is working as a clock and returns value which has to be set in a special label (<strong>label_Time</strong>). This label presents how many seconds left for player's answer in quiz.</p>
  
  <p>The problem is how to automatically change value in label using the timer task? I tried to link value from timer Task (<strong>seconds</strong>) to label_Time value in such a way...</p>
  
  <pre><code>label_Time.textProperty().bind(timer.getSeconds());
  </code></pre>
  
  <p>...but it doesn't work. Is it any way to do this thing? </p>
  
  <p><strong>Thanks in advance for your answer!</strong> :)</p>
  
  <hr>
  
  <p><strong>Initialize method in Controller class:</strong></p>
  
  <pre><code>public void initialize(URL url, ResourceBundle rb) {
  
          Timer2 timer = new Timer2();
          label_Time.textProperty().bind(timer.getSeconds());
          new Thread(timer).start();  
  }
  </code></pre>
  
  <p><strong>Task class "Timer2":</strong></p>
  
  <pre><code>public class Timer2 extends Task{
  
      private static final int SLEEP_TIME = 1000;
      private static int sec;
      private StringProperty seconds;
  
  
      public Timer2(){
          Timer2.sec = 180;
          this.seconds = new SimpleStringProperty("180");
      }
  
      @Override protected StringProperty call() throws Exception {
  
  
          int iterations;
  
          for (iterations = 0; iterations &lt; 1000; iterations++) {
              if (isCancelled()) {
                  updateMessage("Cancelled");
                  break;
              }
  
              System.out.println("TIK! " + sec);
              seconds.setValue(String.valueOf(sec));
              System.out.println("TAK! " + seconds.getValue());
  
              // From the counter we subtract one second
              sec--;
  
              //Block the thread for a short time, but be sure
              //to check the InterruptedException for cancellation
              try {
                  Thread.sleep(10);
              } catch (InterruptedException interrupted) {
                  if (isCancelled()) {
                      updateMessage("Cancelled");
                      break;
                  }
              }
          }
          return seconds;
      }
  
      public StringProperty getSeconds(){
          return this.seconds;
      }
  
  }
  </code></pre>