title: Setting the value of custom javafx ComboBox
body: <p><strong>EDIT:</strong> I have included sample files that give me errors
  I am trying to implement a ComboBox that filters items according to KeyStrokes, following the recipe from <a href="https://stackoverflow.com/questions/13362607/combobox-jump-to-typed-char">here</a>. I 'd like to list countries of the world</p>
  
  <p>I am having the <strong>FilterComboBox.fxml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import java.lang.*?&gt;
  &lt;?import javafx.collections.*?&gt;
  &lt;?import javafx.scene.*?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  &lt;?import persondetails2.controller.*?&gt;
  
  &lt;fx:root type="javafx.scene.control.ComboBox" fx:id="countries" 
           xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" 
           fx:controller="persondetails2.controller.FilterComboBox"&gt;
          &lt;items&gt;
            &lt;FXCollections fx:factory="observableArrayList"&gt;
              &lt;String fx:value="Austria" /&gt;
              &lt;String fx:value="Denmark" /&gt;
              &lt;String fx:value="France" /&gt;
              &lt;String fx:value="Germany" /&gt;
              &lt;String fx:value="Italy" /&gt;
              &lt;String fx:value="Portugal" /&gt;
              &lt;String fx:value="Spain" /&gt;
            &lt;/FXCollections&gt;
          &lt;/items&gt;
  &lt;/fx:root&gt;
  </code></pre>
  
  <p><strong>FilterComboBox.java</strong> as in the recipe. Here is the source:</p>
  
  <pre><code>    /*
   * Follows: https://stackoverflow.com/questions/13362607/combobox-jump-to-typed-char
   */
  
  package persondetails2.controller;
  
  import java.net.URL;
  import java.util.ResourceBundle;
  import javafx.beans.value.ChangeListener;
  import javafx.beans.value.ObservableValue;
  import javafx.collections.FXCollections;
  import javafx.collections.ObservableList;
  import javafx.event.EventHandler;
  import javafx.fxml.FXML;
  import javafx.fxml.FXMLLoader;
  import javafx.fxml.Initializable;
  import javafx.scene.control.ComboBox;
  import javafx.scene.control.SingleSelectionModel;
  import javafx.scene.input.KeyCode;
  import javafx.scene.input.KeyEvent;
  
  /**
   *
   * @author DRY
   */
  public class FilterComboBox&lt;T&gt; extends ComboBox&lt;T&gt; {
  //implements Initializable{
      private final FilterComboBox&lt;T&gt; fcbo = this;
  
      private ObservableList&lt;T&gt; items;
      private ObservableList&lt;T&gt; filter;
      private String s;
      private Object selection;
  
      private class KeyHandler implements EventHandler&lt;KeyEvent&gt; {
  
          private SingleSelectionModel&lt;T&gt; sm;
  
          public KeyHandler() {
              sm = getSelectionModel();
              s = "";
              System.err.println("Initialized keyhandler");
          }
  
          @Override
          public void handle(KeyEvent event) {
              filter.clear();
              // handle non alphanumeric keys like backspace, delete etc
              if (event.getCode() == KeyCode.BACK_SPACE &amp;&amp; s.length() &gt; 0) {
                  s = s.substring(0, s.length() - 1);
              } else {
                  s += event.getText();
              }
  
              if (s.length() == 0) {
                  fcbo.setItems(items);
                  sm.selectFirst();
                  return;
              }
              //System.out.println(s);
              if (event.getCode().isLetterKey()) {
                  for (T item : items) {
                      if (item.toString().toUpperCase().startsWith(s.toUpperCase())) {
  
                          filter.add(item);
                          System.out.println(item);
  
                          fcbo.setItems(filter);
  
                          //sm.clearSelection();
                          //sm.select(item);
  
                      }
                  }
                  sm.select(0);
              }
  
          }
      }
  
      public FilterComboBox(final ObservableList&lt;T&gt; items) {
          super(items);
          this.items = items;
          this.filter = FXCollections.observableArrayList();
  
          setOnKeyReleased(new KeyHandler());
  
          this.focusedProperty().addListener(new ChangeListener() {
              @Override
              public void changed(ObservableValue observable, Object oldValue, Object newValue) {
                  if ((boolean)newValue == false) {
                      s = "";
                      fcbo.setItems(items);
                      fcbo.getSelectionModel().select((T)selection);
                  }
  
              }
  
          });
  
          this.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
              @Override
              public void changed(ObservableValue observable, Object oldValue, Object newValue) {
                  if (newValue != null) {
                      selection = (Object) newValue;
                  }
  
              }
          });
      }
  
  }
  </code></pre>
  
  <p>My main view is called <strong>PersonDetails.fxml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  
  &lt;?import java.lang.*?&gt;
  &lt;?import java.util.*?&gt;
  &lt;?import javafx.collections.*?&gt;
  &lt;?import javafx.geometry.*?&gt;
  &lt;?import javafx.scene.*?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  &lt;?import javafx.scene.text.*?&gt;
  
  
  &lt;AnchorPane xmlns:fx="http://javafx.com/fxml/1" id="AnchorPane" prefHeight="200" prefWidth="320" fx:controller="persondetails2.controller.PersonDetailsController"&gt;
    &lt;ScrollPane id="personalData" fx:id="personaldata" fitToHeight="false" focusTraversable="false" maxHeight="-Infinity" prefHeight="-1.0" prefViewportHeight="1440.0" prefWidth="569.0" AnchorPane.bottomAnchor="2.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="-2.0"&gt;
      &lt;content&gt;
        &lt;AnchorPane id="Content" focusTraversable="false" maxWidth="-1.0" minHeight="0.0" minWidth="0.0" prefHeight="-1.0" prefWidth="600.0"&gt;
          &lt;children&gt;
            &lt;VBox focusTraversable="false" prefHeight="-1.0" prefWidth="-1.0" spacing="2.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"&gt;
              &lt;children&gt;
                &lt;Label prefHeight="21.0001220703125" text="Personal Details"&gt;
                  &lt;font&gt;
                    &lt;Font name="System Bold" size="14.0" fx:id="x1" /&gt;
                  &lt;/font&gt;
                &lt;/Label&gt;
  
                &lt;GridPane id="GridPane" focusTraversable="false" hgap="3.0" prefWidth="354.0" vgap="4.0"&gt;
                  &lt;children&gt;
                    &lt;Label text="Name:" GridPane.columnIndex="0" GridPane.rowIndex="0"&gt;
                      &lt;labelFor&gt;
                        &lt;TextField fx:id="name" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="0" /&gt;
                      &lt;/labelFor&gt;
                    &lt;/Label&gt;
                    &lt;fx:reference source="name" /&gt;
                    &lt;Label text="Surname:" GridPane.columnIndex="0" GridPane.rowIndex="1" /&gt;
                    &lt;TextField fx:id="surname" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1" /&gt;
                    &lt;Label text="Country:" GridPane.columnIndex="0" GridPane.rowIndex="2"  /&gt;
                    &lt;fx:include source="FilterComboBox.fxml" GridPane.columnIndex="1" GridPane.rowIndex="2" /&gt;
                  &lt;/children&gt;
                &lt;/GridPane&gt;
              &lt;/children&gt;
              &lt;padding&gt;
                &lt;Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /&gt;
              &lt;/padding&gt;
            &lt;/VBox&gt;
          &lt;/children&gt;
          &lt;padding&gt;
            &lt;Insets left="5.0" right="5.0" /&gt;
          &lt;/padding&gt;
        &lt;/AnchorPane&gt;
      &lt;/content&gt;
    &lt;/ScrollPane&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  
  <p>and the respective controller is <strong>PersonDetailsController.java</strong></p>
  
  <pre><code>package persondetails2.controller;
  
  import java.net.URL;
  import java.util.ResourceBundle;
  import javafx.event.ActionEvent;
  import javafx.fxml.FXML;
  import javafx.fxml.Initializable;
  import javafx.scene.control.Label;
  
  /**
   *
   * @author DRY
   */
  public class PersonDetailsController implements Initializable {
  
      @FXML
      private Label label;
  
      @FXML
      private void handleButtonAction(ActionEvent event) {
          System.out.println("You clicked me!");
          label.setText("Hello World!");
      }
  
      @Override
      public void initialize(URL url, ResourceBundle rb) {
          // TODO
      }    
  
  }
  </code></pre>
  
  <p>This configuration gives me now an <code>java.lang.InstantiationException: persondetails2.controller.FilterComboBox</code>. I would like to read the data in the controller initialisation and then be able to set the values in the TextFields as well as the chosen value in the ComboBox. </p>
  
  <p>Any ideas on how to continue?</p>