title: How to bind multiple properties to one observable in JavaFX
body: <p><strong>Update 3: solution</strong></p>
  
  <p>I finally got it: just save a reference of <strong>MyRoot</strong> in a property:</p>
  
  <p><strong>MainApp</strong></p>
  
  <pre><code>package my.group.javafxtestbinding;
  
  import javafx.application.Application;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.scene.layout.HBox;
  import javafx.stage.Stage;
  
  public class MainApp extends Application {
  
      private MyRoot root;
  
      @Override
      public void start(Stage primaryStage) {
          root = new MyRoot();
          HBox parent = root.myRoot;
          Scene scene = new Scene(parent, 600, 400);
          primaryStage.setScene(scene);
          primaryStage.show();
  
          Button gcButton = new Button("Collect Garbage");
          gcButton.setOnMousePressed(event -&gt; System.gc());
          parent.getChildren().add(gcButton);
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>
  
  <p><strong>Update 3 solution end</strong></p>
  
  <p><strong>Update 2:</strong></p>
  
  <p>So, I'm still having problems with losing my bindings and it's due to garbage collection; that I can reproduce. See the follwing Minimal Working Example.</p>
  
  <p>The structure of the example is somewhat special, that is because I use the MVP framework from Adam Bien: <a href="http://afterburner.adam-bien.com/" rel="nofollow noreferrer">afterburner.fx</a>. And I wanted to simulate the same structure in my example.</p>
  
  <p>I read a lot (i.e. <a href="https://stackoverflow.com/questions/23785816/javafx-beans-binding-suddenly-stops-working">here</a>, <a href="https://tomasmikula.github.io/blog/2015/02/10/the-trouble-with-weak-listeners.html" rel="nofollow noreferrer">here</a>) and understand the basic problem but still can't really grasp it.<br>
  I'm somewhat new to JavaFX, hope somebody can give me a hint.</p>
  
  <p>Reproduce:<br>
  When you click on an item in the table, the content of the label changes due to the binding between the selected model in the table (<strong>Select</strong>) and the label (<strong>Show</strong>). <strong>MyRoot</strong> manages both.<br>
  When you click on the button garbage collection is executed and the binding is gone.</p>
  
  <p><strong>MainApp</strong></p>
  
  <pre><code>package my.group.javafxtestbinding;
  
  import javafx.application.Application;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.scene.layout.HBox;
  import javafx.stage.Stage;
  
  public class MainApp extends Application {
  
      @Override
      public void start(Stage primaryStage) {
          MyRoot root = new MyRoot();
          HBox parent = root.myRoot;
          Scene scene = new Scene(parent, 600, 400);
          primaryStage.setScene(scene);
          primaryStage.show();
  
          Button gcButton = new Button("Collect Garbage");
          gcButton.setOnMousePressed(event -&gt; System.gc());
          parent.getChildren().add(gcButton);
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>
  
  <p><strong>MyRoot</strong></p>
  
  <pre><code>package my.group.javafxtestbinding;
  
  import javafx.scene.layout.HBox;
  
  public class MyRoot {
  
      public HBox myRoot;
      private final Select select;
      private final Show show;
  
      public MyRoot() {
          select = new Select();
          show = new Show();
          myRoot = new HBox();
          myRoot.getChildren().addAll(select.myTable, show.myLabel);
  
          show.selectedModelProperty().bind(select.selectedModelProperty());
      }
  }
  </code></pre>
  
  <p><strong>Select</strong></p>
  
  <pre><code>package my.group.javafxtestbinding;
  
  import javafx.beans.property.ObjectProperty;
  import javafx.beans.property.SimpleObjectProperty;
  import javafx.beans.value.ChangeListener;
  import javafx.beans.value.ObservableValue;
  import javafx.collections.FXCollections;
  import javafx.collections.ObservableList;
  import javafx.scene.control.TableColumn;
  import javafx.scene.control.TableView;
  
  public class Select {
      private ObjectProperty&lt;MyModel&gt; selectedModel;
      public TableView&lt;MyModel&gt; myTable;
      private final TableColumn&lt;MyModel, String&gt; myColumn;
  
      public Select() {
          this.selectedModel = new SimpleObjectProperty&lt;&gt;();
  
          myTable = new TableView();
          myColumn = new TableColumn("Name");
          myTable.getColumns().add(myColumn);
  
          myTable.setItems(getList());
          myColumn.setCellValueFactory(cellData -&gt; cellData.getValue().getNameProperty());
  
          ChangeListener&lt;MyModel&gt; changeListener = new ChangeListener&lt;MyModel&gt;() {
              @Override
              public void changed(ObservableValue&lt;? extends MyModel&gt; observable, MyModel oldValue, MyModel newValue) {
                  selectedModel.set(newValue);
              }
          };
          myTable.getSelectionModel().selectedItemProperty().addListener(changeListener);
      }
  
      public ObjectProperty&lt;MyModel&gt; selectedModelProperty() {
          return selectedModel;
      }
  
      private ObservableList&lt;MyModel&gt; getList() {
        MyModel model1 = new MyModel("Joe");
        MyModel model2 = new MyModel("Jim");
        MyModel model3 = new MyModel("Jack");
        ObservableList&lt;MyModel&gt; list = FXCollections.observableArrayList(model1, model2, model3);
        return list;
    }
  }
  </code></pre>
  
  <p><strong>Show</strong></p>
  
  <pre><code>package my.group.javafxtestbinding;
  
  import javafx.beans.property.ObjectProperty;
  import javafx.beans.property.SimpleObjectProperty;
  import javafx.beans.value.ChangeListener;
  import javafx.beans.value.ObservableValue;
  import javafx.scene.control.Label;
  
  public class Show {
      private ObjectProperty&lt;MyModel&gt; selectedModel;
      public Label myLabel;
  
      public Show() {
          this.selectedModel = new SimpleObjectProperty&lt;&gt;();
  
          myLabel = new Label("Init");
  
          ChangeListener listener = new ChangeListener&lt;MyModel&gt;() {
              @Override
              public void changed(ObservableValue&lt;? extends MyModel&gt; observable, MyModel oldValue, MyModel newValue) {
                  if (newValue != null) {
                      myLabel.setText(newValue.getName());
                  }
              }
          };
          this.selectedModel.addListener(listener);
      }
  
      public ObjectProperty&lt;MyModel&gt; selectedModelProperty() {
          return selectedModel;
      }
  }
  </code></pre>
  
  <p><strong>MyModel</strong></p>
  
  <pre><code>package my.group.javafxtestbinding;
  
  import javafx.beans.property.SimpleStringProperty;
  import javafx.beans.property.StringProperty;
  
  public class MyModel {
      private final StringProperty name;
  
      public MyModel(String name) {
          this.name = new SimpleStringProperty(name);
      }
  
      public String getName() {
          return name.get();
      }
  
      public void setName(String name) {
          this.name.set(name);
      }
  
      public StringProperty getNameProperty() {
          return name;
      }
  }
  </code></pre>
  
  <p><strong>Update 2 end</strong></p>
  
  <p><strong>Update 1:</strong></p>
  
  <p>Ok, thank you for your comments. </p>
  
  <p>I thought I was doing it wrong. But if you say that this is the correct way then the problem must be somewhere else. I will post it when I find the problem...</p>
  
  <p><strong>Update 1 end</strong></p>
  
  <p><strong>Original Post:</strong></p>
  
  <p>I have problems binding many properties to one observable.</p>
  
  <p>Let's say I have three (or more) properties <code>ObjectProperty&lt;Foo&gt; firstProp</code> in <code>FirstObject</code>, <code>ObjectProperty&lt;Foo&gt; secondProp</code> in <code>SecondObject</code> and <code>ObjectProperty&lt;Foo&gt; thirdProp</code> in <code>ThirdObject</code>.</p>
  
  <p>How can I bind second and third property to the first which is the observable.
  So when firstProp changes the ChangeListeners attached to secondProp and thirdProp will be fired.</p>
  
  <p>What I tried:</p>
  
  <pre><code>SecondObject.secondProperty().bind(FirstObject.firstProperty());
  ThirdObject.thirdProperty().bind(FirstObject.firstProperty());
  </code></pre>
  
  <p>The result is that it sometimes works and sometimes not. If I add more bindings in the same way they do not work at all.</p>
  
  <p>Is this not working because of this:</p>
  
  <blockquote>
    <p>Note that JavaFX has all the bind calls implemented through weak listeners. This means the bound property can be garbage collected and stopped from being updated.
    <a href="http://docs.oracle.com/javase/8/javafx/api/javafx/beans/property/Property.html#bind-javafx.beans.value.ObservableValue-" rel="nofollow noreferrer">Javadoc bind()</a></p>
  </blockquote>
  
  <p>I find a lot of answers to how to bind many observables to one property, but not the other way.
  Can somebody point me in the right direction?</p>