I am trying to make a SuggestBox showing suggestions only after 2 characters have been typed. My idea was to hide the suggestions when the text length is 1, using the class DefaultSuggestionDisplay. I have tried to attach different handlers like KeyPressHandler and KeyUpHandler on the SuggestionBox itself and its TextBox, but none of them seemed to work. Do you have any "suggestions"? :D
How to show suggestions in a GWT SuggestBox only after 2 characters have been typed?
1.1k Views Asked by AndreiXwe At
2
There are 2 best solutions below
4
On
You can extend SuggestBox and override showSuggestionList() method.
Adding KeyUpHandler is not working because you add another KeyUpHandler, not replacing the one that SuggestBox added to its own TextBox.
EDIT:
@Override
showSuggestionList() {
if (getTextBox().getValue().length() > 1) {
super.showSuggestionList();
}
}
You can extend
DefaultSuggestionDisplayand overrideshowSuggestionsmethod:You have to pass your new display to the
SuggestBoxconstructor:In this constructor you should provide:
SuggestOracleclass (here namedMySuggestOracle) - I suppose you have oneTextBox- it is the default widget to enter text (you can provide your own, it just needs to implementHasText)SuggestionDisplay- use the one withshowSuggestionsmethod overridden.This is full working example code showing suggestions on at least 2 characters typed: