title: javafx create ComboBox TableCell
body: <p>I'm trying to create a custom TableCell in my TableView. I'd like it to display a ComboBox where I can choose a String value, and then display the String value as if it was an user input. The idea is that ths user doesn't know which are the allowed values so he can simply pick one of them in the ComboBox.</p>
  
  <p>I tried to do that making my own "ComboBoxCell" but it doesn't work as expected : </p>
  
  <pre><code>public class ComboBoxCell extends TableCell&lt;ClassesProperty, String&gt; {
  
      private ComboBox&lt;String&gt; comboBox;
  
      public ComboBoxCell() {
      }
  
      @Override
      public void startEdit() {
          super.startEdit();
  
          if (comboBox == null) {
              createComboBox();
          }
  
          setGraphic(comboBox);
          setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
      }
  
      @Override
      public void cancelEdit() {
          super.cancelEdit();
  
          setText(String.valueOf(getItem()));
          setContentDisplay(ContentDisplay.TEXT_ONLY);
      }
  
      public void updateItem(String item, boolean empty) {
          super.updateItem(item, empty);
  
          if (empty) {
              setText(null);
              setGraphic(null);
          } else {
              if (isEditing()) {
                  if (comboBox != null) {
                      comboBox.setValue(getString());
                  }
                  setGraphic(comboBox);
                  setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
              } else {
                  setText(getString());
                  setContentDisplay(ContentDisplay.TEXT_ONLY);
              }
          }
      }
  
      private void createComboBox() {
          // ClassesController.getLevelChoice() is the observable list of String
          comboBox = new ComboBox&lt;&gt;(ClassesController.getLevelChoice());
          comboBox.setMinWidth(this.getWidth() - this.getGraphicTextGap()*2);
          comboBox.setOnKeyPressed(new EventHandler&lt;KeyEvent&gt;() {
              @Override
              public void handle(KeyEvent t) {
                  if (t.getCode() == KeyCode.ENTER) {
                      commitEdit(comboBox.getSelectionModel().getSelectedItem());
                  } else if (t.getCode() == KeyCode.ESCAPE) {
                      cancelEdit();
                  }
              }
          });
      }
  
      private String getString() {
          return getItem() == null ? "" : getItem().toString();
      }
  }
  </code></pre>
  
  <p>Then in my "main" app :</p>
  
  <pre><code>levelChoice = FXCollections.observableArrayList(
          new String("Bla"),
          new String("Blo")
      );
  
  // Level Column : String value
  Callback&lt;TableColumn, TableCell&gt; comboBoxFactory = new Callback&lt;TableColumn, TableCell&gt;() {
          @Override
          public TableCell call(TableColumn p) {
              return new ComboBoxCell();
          }
      };
  
  levelColumn.setCellValueFactory(
          new PropertyValueFactory&lt;ClassesProperty, String&gt;("level")
      );
  levelColumn.setCellFactory(comboBoxFactory);
  </code></pre>
  
  <p>Any ideas?
  Thanks !</p>