title: Adding new elements on ComboBox
body: <p>I'm developing a <code>Vaadin</code> app, and I've decided to use the <code>SuggestingComboBox</code> idea, that allows better searches between items. This works great, but now I can't figure out how to add new elements, that aren't already in the container with the <code>setNewItemHandler</code> method.<br>
  For refference, here is the <code>SuggestingComBox</code> code:</p>
  
  <pre><code>public class SuggestingComboBox extends ComboBox {
  
      public SuggestingComboBox() {
          setItemCaptionMode(ItemCaptionMode.PROPERTY);
          setItemCaptionPropertyId("name");
      }
  
      @Override
      protected Filter buildFilter(String filterString, 
              FilteringMode filteringMode) {
          return new SuggestingContainer.SuggestionFilter(filterString);
      }
  }
  </code></pre>
  
  <p>And the <code>SuggestingContainer</code> code, that comes along the combobox:</p>
  
  <pre><code>public abstract class SuggestingContainer&lt;T&gt; extends BeanItemContainer&lt;T&gt; {
  
      public SuggestingContainer(Class&lt;? super T&gt; type)
              throws IllegalArgumentException {
          super(type);
      }
  
      @Override
      protected void addFilter(Filter filter) 
              throws UnsupportedFilterException {
          if(filter instanceof SuggestionFilter) {
              SuggestionFilter suggestionFilter = (SuggestionFilter) filter;
              filterItems(suggestionFilter.getFilterString());
          } else 
              super.addFilter(filter);
      }
  
      //This method is to be overriden
      protected abstract void filterItems(String filterString);
  
     public static class SuggestionFilter implements Container.Filter {
         private String filterString;
  
         public SuggestionFilter(String filterString) {
             this.filterString = filterString;
         }
  
         public String getFilterString() {
             return filterString;
         }
  
         @Override
         public boolean passesFilter(Object itemId, Item item) 
                 throws UnsupportedOperationException {
             return false;
         }
  
         @Override
         public boolean appliesToProperty(Object propertyId) {
             return false;
         }
    }
  </code></pre>
  
  <p>This container is used as this:</p>
  
  <pre><code>public SuggestingField extends CustomField&lt;Model&gt; {
      private SuggestingComboBox suggestingCB;
      private SuggestingContainer&lt;ObjectItem&gt; container;
  
      public SuggestingField() {
          suggestingCB = new SuggestingComboBox();
          container = new SuggestingContainer&lt;ObjectItem&gt;(ObjectItem.class) {
              @Override
              protected void filterItems(String filterString) {               
                  removeAllItems();
  
                  List&lt;Model&gt; result; //Obtain here the results
                  List&lt;ObjectItem&gt; lItems = result.stream()
                          .map(m -&gt; new ObjectItem(m.getId()+"", m.toString(), m))
                          .collect(Collectors.toList());
                  addAll(lItems);
              }
          };
          suggestingCB.setContainerDataSource(container);
          suggestingCB.setImmediate(true);
          suggestingCB.setItemCaptionPropertyId("text");
      }
  }
  </code></pre>
  
  <p>Also, I have a POJO <code>Model</code> and that <code>ObjectItem</code> class, that I have to use, beacuse I don't get to work the <code>toString</code> caption mode.</p>
  
  <pre><code>public class Model {
      private int id;
      private String name;
      //Getters and setters; equals/hash
  }
  public class ObjectItem {
      private String id;
      private String text;
      private Object o;
      //Equals/hash and getters/setters
  }
  </code></pre>
  
  <p>So, <strong>my problem is</strong>: I'm trying to allow creating new <code>Model</code>(s) in the field, using the <code>setNewItemHandler</code> method. This is what I've tried so far.</p>
  
  <pre><code>public SuggestingField extends CustomField&lt;Model&gt; {
  ...
      public void setNewElementsAllowed(boolean allowed) {
          this.suggestingCB.setNewItemsAllowed(allowed);
          this.suggestingCB.setImmediate(allowed);
          if(allowed) {
              suggestingCB.setNewItemHandler(caption -&gt; {
                  Model m = new Model();
                  m.setNombre(caption);
  
                  setValue(m);
              });
          }
      }
      public void setValue(Model m) {
          super.setValue(m);
          if(m == null)
              suggestingCB.setValue(null);
          else {
              ObjectItem it = new ObjectItem(""+m.getId(),
                  m.getName(), m);
              if(container.size() == 0 || !container.containsId(it.getId()))
                  container.addItem(it);
  
              suggestingCB.setValue(it);
              suggestingCB.select(it);
          }
      }
  </code></pre>
  
  <p>After debuggind this code, I've found that the <code>newElementHandler</code> is used, and it adds a new item to the container, but nothing is shown in the <code>ComboBox</code> and the next handler's execution, the container is empty, as if no item was inserted. </p>