Search code examples
gwtsuggestbox

How to show suggestions in a GWT SuggestBox only after 2 characters have been typed?


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


Solution

  • You can extend DefaultSuggestionDisplay and override showSuggestions method:

    public class MySuggestionDisplay extends DefaultSuggestionDisplay {
        @Override
        protected void showSuggestions(SuggestBox suggestBox, Collection<? extends Suggestion> suggestions, boolean isDisplayStringHTML, boolean isAutoSelectEnabled, SuggestionCallback callback) {
            if(suggestBox.getText().length() > 1)
                super.showSuggestions(suggestBox, suggestions, isDisplayStringHTML, isAutoSelectEnabled, callback);
        }
    }
    

    You have to pass your new display to the SuggestBox constructor:

    public class MySuggestBox extends SuggestBox {
        public MySuggestBox() {
            super(
                new MySuggestOracle(),
                new TextBox(), 
                new MySuggestionDisplay());
        }
    }
    

    In this constructor you should provide:

    • your own SuggestOracle class (here named MySuggestOracle) - I suppose you have one
    • TextBox - it is the default widget to enter text (you can provide your own, it just needs to implement HasText)
    • SuggestionDisplay - use the one with showSuggestions method overridden.

    This is full working example code showing suggestions on at least 2 characters typed:

    import java.util.ArrayList;
    import java.util.Collection;
    
    import com.google.gwt.user.client.ui.SuggestBox;
    import com.google.gwt.user.client.ui.SuggestOracle;
    import com.google.gwt.user.client.ui.SuggestOracle.Suggestion;
    import com.google.gwt.user.client.ui.TextBox;
    
    public class MySuggestBox extends SuggestBox {
    
        public MySuggestBox() {
            super(
                new SuggestOracle() {
                    @Override
                    public void requestSuggestions(Request request, Callback callback) {
                        ArrayList<Suggestion> suggestions = new ArrayList<Suggestion>();
                        suggestions.add(new MySuggestion("aaa"));
                        suggestions.add(new MySuggestion("bbb"));
                        suggestions.add(new MySuggestion("ccc"));
                        suggestions.add(new MySuggestion("ddd"));
    
                        Response response = new Response();
                        response.setSuggestions(suggestions);
                        callback.onSuggestionsReady(request, response);
                    }
                }, 
                new TextBox(), 
                new MySuggestionDisplay());
        }
    
        public static class MySuggestionDisplay extends DefaultSuggestionDisplay {
            @Override
            protected void showSuggestions(SuggestBox suggestBox, Collection<? extends Suggestion> suggestions, boolean isDisplayStringHTML, boolean isAutoSelectEnabled, SuggestionCallback callback) {
                if(suggestBox.getText().length() > 1)
                    super.showSuggestions(suggestBox, suggestions, isDisplayStringHTML, isAutoSelectEnabled, callback);
            }
        }
    
        public static class MySuggestion implements Suggestion {
    
            private String text;
    
            public MySuggestion(String text) {
                this.text = text;
            }
    
            @Override
            public String getDisplayString() {
                return text;
            }
    
            @Override
            public String getReplacementString() {
                return text;
            }
        }
    }