title: JavaFX ComboBox binding
body: <p>lets say that I have two models. Teacher and Students and each Student can have one Teacher (not several). So I want to have an combo box on my student pane where I can select one teacher.</p>
  
  <p>Both models are also stored in the database and I want only the database ID of the teacher to be in the model of the student but inside the combo box the name of the teacher should appear.</p>
  
  <p>Also the model should be binded to the combo box, so if somebody changed the teacher in the combobox, the model (of the student) should be refreshed as well. With textfields I can bind them to StringProperty objects, but in this case I need to bind the comboxbox item (Teacher.java) to an interger-property inside my Student.java.</p>
  
  <p>I also thought about having the teacher-model as a property inside my student class, but I think that will not help, because then I need to bind the combobox item (teacher.java) with a teacher object inside my student model but only <em>property</em> objects can be binded.</p>
  
  <p><strong>Teacher.java</strong></p>
  
  <pre><code>import javafx.beans.property.IntegerProperty;
  import javafx.beans.property.SimpleIntegerProperty;
  import javafx.beans.property.SimpleStringProperty;
  import javafx.beans.property.StringProperty;
  
  public class Teacher {
  
      private Integer databaseID;
  
      private StringProperty name = new SimpleStringProperty();
  
      private IntegerProperty age = new SimpleIntegerProperty();
  
  
      public Teacher (Integer databaseID) {
          // load data from database and fill into model
      }
  
      public void store() {
          // write model to database
      }
  
      // getters and setters ...
  
      @Override
      public String toString() {
          return name.get();
      }
  
      @Override
      public boolean equals(Object obj) {
          // compare databaseIDs ...
          return true;
      }
  }
  </code></pre>
  
  <p><strong>Student.java</strong></p>
  
  <pre><code>import javafx.beans.property.SimpleStringProperty;
  import javafx.beans.property.StringProperty;
  
  public class Student {
  
      private Integer databaseID;
  
      private StringProperty name = new SimpleStringProperty();
  
      private Integer teacherID;
  
  
      public Student (Integer databaseID) {
          // load data from database and fill into model
      }
  
      public void store() {
          // write model to database
      }
  
      // getters and setters ...
  
      @Override
      public String toString() {
          return name.get();
      }
  
      @Override
      public boolean equals(Object obj) {
          // compare databaseIDs ...
          return true;
      }
  }
  </code></pre>
  
  <p><strong>Application.java</strong></p>
  
  <pre><code>import javafx.application.Application;
  import javafx.scene.Scene;
  import javafx.scene.control.ComboBox;
  import javafx.scene.layout.StackPane;
  import javafx.stage.Stage;
  
  public class TestApplication extends Application {
  
      public static void main(String[] args) {
          launch(args);
      }
  
      @Override
      public void start(Stage primaryStage) {
  
          Student student = new Student(4711);
  
          ComboBox&lt;Teacher&gt; teachers = new ComboBox&lt;Teacher&gt;();
          fillTeacherComboBox(teachers);
  
          // TODO: select the teacher from the student by default
  
          // FIXME: Binding student.databaseID &lt;--&gt; ComboBox-Item.databaseID
  
  
          StackPane root = new StackPane();
          root.getChildren().add(teachers);
          primaryStage.setScene(new Scene(root, 300, 250));
          primaryStage.show();
      }
  
      private void fillTeacherComboBox(ComboBox&lt;Teacher&gt; teachers) {
          // TODO: Load data from database and fill teacher combo box
      }
  
  
  }
  </code></pre>