title: JavaFX ComboBox with CheckBoxes
body: <h3>Description of the Question</h3>
  
  <p>I try to create a <code>JavaFX</code> <code>ComboBox</code> which holds <code>CheckBoxes</code> in the dropdown menu.
  The <code>ComboBox</code> shall be editable and gets fed by a simple <code>Class</code> lets call it <code>CheckItem</code>.
  the list of <code>CheckItems</code> shall be checkable - and shall not close the dropdown menu after a selection is made.</p>
  
  <p>finally the Text in the <code>ComboBox</code> should be available and a Selection (all checked Items)</p>
  
  <h3>this is what i already worked out</h3>
  
  <p>(1) a <code>ComboBox</code> rendering the <code>CheckItem</code> as <code>CheckedBox</code> with correct selection</p>
  
  <p>(2) gaining the Text from the <code>ComboBox</code></p>
  
  <h3>problems coming up</h3>
  
  <p>(1) After clicking on one item the dropdown closes &amp; selection state of the item does not change.</p>
  
  <p>(2) As far as i noticed its only possible to have <strong>one</strong> item selected at a time?</p>
  
  <p>here is my code for testing the stuff:</p>
  
  <h3>Test Program</h3>
  
  <pre><code>public class ComboButtonSample extends Application {
  
      @Override
      public void start(Stage stage) {
              final ObservableList&lt;CheckItem&gt; items = fetchItems();
              ComboBox&lt;CheckItem&gt; combo = createComboBox(items);
              combo.setPromptText("enter searchstring here");
              combo.setEditable(true);
  
  
              // order the components vertically
              VBox vBox = new VBox();
              vBox.getChildren().add(combo);
  
              // Button to write out the text and the items of the combobox
              Button btn = new Button();
              btn.setText("combo text to console");
              btn.setOnAction((event) -&gt; {
                      System.out.println("Text is: "+combo.getEditor().getText());
                      System.out.println("Content is: ");
                      for (Iterator&lt;CheckItem&gt; iterator = combo.getItems().iterator(); iterator.hasNext();) {
                              CheckItem ci = (CheckItem) iterator.next();
                              System.out.println(String.format("[%s] %s -&gt; %s", ci.selected ? "X" : " ",ci.getDisplayName(), ci.getInternalName()));
  
                      }
              });
  
              vBox.getChildren().add(btn);
  
              // show you do not need any code to change the selection of the box.
              CheckBox checkBox = new CheckBox();
              checkBox.setText("test box");
              vBox.getChildren().add(checkBox);
  
              stage.setScene(new Scene(vBox));
              stage.show();
      }
  
      private ComboBox&lt;CheckItem&gt; createComboBox(ObservableList&lt;CheckItem&gt; data) {
              ComboBox&lt;CheckItem&gt; combo = new ComboBox&lt;&gt;();
              combo.getItems().addAll(data);
              combo.setCellFactory(listView -&gt; new CheckItemListCell());
              return combo;
      }
  
      class CheckItemListCell extends ListCell&lt;CheckItem&gt; {
              private final CheckBox btn;
  
              CheckItemListCell() {
                      setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                      btn = new CheckBox();
              }
  
              @Override
              protected void updateItem(CheckItem item, boolean empty) {
                      super.updateItem(item, empty);
  
                      if (item == null || empty) {
                              setGraphic(null);
                      } else {
                              btn.setText(item.getDisplayName());
                              btn.selectedProperty().setValue(item.selected);
                              setGraphic(btn);
                      }
              }
      }
  
      private ObservableList&lt;CheckItem&gt; fetchItems() {
              final ObservableList&lt;CheckItem&gt; data = FXCollections
                              .observableArrayList();
              for (int i = 1; i &lt; 15; i++) {
                      CheckItem chkItem = new CheckItem();
                      chkItem.selected = i%3==0;
                      chkItem.setDisplayName("DisplayName" + i);
                      chkItem.setInternalName("InternalName" + i);
                      data.add(chkItem);
              }
              return data;
      }
  
      public static void main(String[] args) {
              launch(args);
      }
  </code></pre>
  
  <h3>CheckItem</h3>
  
  <pre><code>public class CheckItem {
      boolean selected;
      String displayName;
      String internalName;    
  
      public boolean isChecked() {
          return selected;
      }
  
      public void setChecked(boolean checked) {
          this.selected = checked;
      }
  
      public String getDisplayName() {
          return displayName;
      }
  
      public void setDisplayName(String displayName) {
          this.displayName = displayName;
      }
  
      public String getInternalName() {
          return internalName;
      }
  
      public void setInternalName(String internalName) {
          this.internalName = internalName;
      }
  }
  </code></pre>