Search code examples
javagwthandlersuggestbox

GWT- Suggestbox listener not working


I need to add a handler that fires when a selection is CLICKED which will then validate the value. Current functionality is validating (through textInput on blur) right before the entire value is recorded from the suggestbox, thus not passing validation (when it should).

Here is what i tried right below where i implement the suggestbox in the TextInput page:

public void onModuleLoad() {

SuggestBox box = new SuggestBox(createListOracle(),myTextBox());

box.addSelectionHandler(new SelectionHandler<Suggestion>() {

    @Override
    public void onSelection(SelectionEvent<Suggestion> event) {
        Validate();
    }
});

another solution could be to insert the courser on focus when suggestbox is selected from, that would accomplish the same thing for me.

The problem is the handler is never firing. The break-point is never reached.


Solution

  • Then take a look at ValueBoxBase.

    You will pass your own instance to the constructor of the SuggestBox

    public SuggestBox(SuggestOracle oracle, ValueBoxBase box)

    TextBox is a subclass of ValueBoxBase, and it has ClickListeners so you have the choice between:

    1. Create your TextBox outside and add it listeners, then pass it to the constructor SuggestBox(SuggestOracle oracle, ValueBoxBase box)
    2. Overriding SuggestBox and making the constructor take a "better" ValueBoxBase (for example TextBox) and add the listener methods to your implmentation

    I tried this sample, it works

            TextBox suggestTextBox = new TextBox();
        suggestTextBox.addClickHandler(new ClickHandler() {
    
            @Override
            public void onClick(ClickEvent event) {
                Window.alert("tada");
            }
        });
        SuggestOracle oracle = new MultiWordSuggestOracle(" ,");
    
        final SuggestBox nameField = new SuggestBox(oracle, suggestTextBox);