title: JavaFX custom ComboBox with generic class
body: <p>I have two types of classes which extend class X</p>
  
  <pre><code>public class A extends X
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public class B extends X
  </code></pre>
  
  <p>Within my JavaFX application, there needs to be a combobox. This comboBox is constructed by using either classes A or B, and uses the respective toString() method of that class to populate. To cut down on code duplication.</p>
  
  <p>I'm trying to amend my current method for generating the comboBox, such that it will allow an ObservableList of type A or B.</p>
  
  <pre><code>protected ComboBox&lt;? extends X&gt; getComboBox(ObservableList&lt;? extends X&gt; list){
      ComboBox&lt;? extends X&gt; comboBox = new ComboBox&lt;&gt;();
      comboBox.setItems(list);
      comboBox.setCellFactory(new Callback&lt;ListView&lt;? extends X&gt;, ListCell&lt;? extends X&gt;&gt;() {
          @Override
          public ListCell&lt;Controller&gt; call(ListView&lt;Controller&gt; param) {
  
              return new ListCell&lt;? extends X&gt;(){
                  @Override
                  public void updateItem(Class&lt;? extends X&gt; content, boolean empty){
                      super.updateItem(content, empty);
                      if(!empty) {
                          setText(content.toString());
                      } else {
                          setText(null);
                      }
                  }
              };
          }
      });
      return comboBox;
  }
  </code></pre>
  
  <p>Since both the classes have a superclass, is it possible to achieve something like this (And potentially, but not essentially, return it as it's original class type)</p>
  
  <p>@SillyFly - I retrieve the list using the Reflections dependency. It returns all classes of a specific type (i.e. A)</p>
  
  <pre><code>public ObservableList&lt;A&gt; getAList() {
      Reflections reflection = new Reflections("com.classes.aClasses");
      ObservableList&lt;A&gt; classesList = FXCollections.observableArrayList();
      for (Class&lt;? extends A&gt; thisClass : reflection.getSubTypesOf(A.class)) {
          try {
              classesList.add(thisClass.newInstance());
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
      return classesList;
  }
  </code></pre>