title: GWT SuggestBox: How do I force the SuggestBox to select the first item in the suggestion list?
body: <p>I have a textbox and one suggestbox. I attach a value change and key up handler to the text box such that whatever the user types (or pastes) into the text box is echo-ed inside the suggestbox. I can get the suggestbox to display the suggestion list by calling showSuggestionList on each value change and key up event.</p>
  
  <p>Now, how do I get the suggestbox to automatically choose the first item in the suggestion list?</p>
  
  <p>One of the methods I tried is to programatically simulate key presses,  i.e</p>
  
  <pre><code>suggestBox.setFocus(true);
  NativeEvent enterEvent = Document.get().createKeyPressEvent(false, false, false,   false, KeyCodes.KEY_ENTER);
  DomEvent.fireNativeEvent(enterEvent, suggestBox);
  textBox.setFocus(true);
  </code></pre>
  
  <p>This doesn't work at all. The enter key isn't simulated. Another possible solution is to extend SuggestionBox.SuggestionDisplay, but I'm not too sure how to that. Any pointers appreciated.</p>
  
  <p>Update: I'm still working on this and trying various methods.</p>
  
  <p>Here, I tried to implement my own SuggestionDisplay by subclassing DefaultSuggestionDisplay and overriding getCurrentSelection() to make accessible from my class. This doesn't work either. Null is returned.</p>
  
  <pre><code>    private class CustomSuggestionDisplay extends DefaultSuggestionDisplay {
      @Override
      protected Suggestion getCurrentSelection() {
          return super.getCurrentSelection();
      }
  }
  
      suggestBox.setAutoSelectEnabled(true);
  
      textBox.addKeyUpHandler(new KeyUpHandler() {
  
          public void onKeyUp(KeyUpEvent event) {
              suggestBox.setValue(textBox.getText(), true);
              suggestBox.showSuggestionList();
              if (suggestBox.isSuggestionListShowing()) {
                  String s = ((CustomSuggestionDisplay) suggestBox.getSuggestionDisplay()).getCurrentSelection().getDisplayString();
                  Window.alert(s);
              }
          }
      });
  </code></pre>
  
  <p>Here, I tried to attach a value change handler to the SuggestBox, and casting the event type to SuggestOracle.Suggestion. Again, null is returned.</p>
  
  <pre><code>suggestBox.addValueChangeHandler(new ValueChangeHandler&lt;String&gt;() {
  
          public void onValueChange(ValueChangeEvent&lt;String&gt; event) {
              String s = ((SuggestOracle.Suggestion) event).getDisplayString();
              Window.alert(s);
          }
      });
  </code></pre>