8

Given the following code:

    TextBox tb = new TextBox();
    tb.addValueChangeHandler(new ValueChangeHandler<String>() {

        @Override
        public void onValueChange(ValueChangeEvent<String> event) {
            Window.alert(event.getValue());

        }
    });

onValueChange will be called if the TextBox loses focus. I am trying to have it called whenever the input value changes. The solutions I have read all involve handling the keyup and paste events such as in this answer. How to build a Facebook-style input box in GWT Hasn't this been addresses in GWT 2.5.1? Is there a way to bind to the native input change method? Another widget or using the UI framework would be acceptable if they have this functionality

Community
  • 1
  • 1
Nathaniel Johnson
  • 4,731
  • 1
  • 42
  • 69
  • 2
    I really do think that the keyup handler is the best way to go at the moment. – enrybo Oct 05 '13 at 18:58
  • Does this answer your question? [Instant value change handler on a GWT textbox](https://stackoverflow.com/questions/3184648/instant-value-change-handler-on-a-gwt-textbox) – Luzifer42 Nov 10 '21 at 09:45

4 Answers4

5

The only way in GWT is to bind to the key down/key up and paste events.

Please send a patch to add support for the input event (using addBitlessDomHandler): http://www.gwtproject.org/makinggwtbetter.html, it should be rather easy (add a acase in DOMImpl) and has good chances to make it in (and if sent early enough, would have good chances to ship in GWT 2.6)

Thomas Broyer
  • 64,353
  • 7
  • 91
  • 164
4

The solution is use the right event and that is the input event, so look how to hook on it, there are many ways to do it. Because I work with elements and not with Widgets this the code that I use it:

    Element input = Document.get().getElementById("my-input");
    DOM.sinkBitlessEvent(input, "input");
    DOM.setEventListener(input, event -> GWT.log("Event!"));
Daniel De León
  • 13,196
  • 5
  • 87
  • 72
0

I used the following to detect when the value of a textbox changes. Just bare in mind this only detects if the text box goes from empty to full or full to empty. This was done to try reduce async calls to the server to validate the input

@UiHandler("notes")
    @SuppressWarnings("unused")
    public void notes(KeyUpEvent event) {
        if (!areNotesCaptured && !notes.getText().isEmpty()){
            fireLeaveSelectionUpdatedEvent();
            areNotesCaptured = true;
        } else if (areNotesCaptured && notes.getText().isEmpty()){
           fireLeaveSelectionUpdatedEvent();
            areNotesCaptured = false;
        }
    }
Donovan Thomson
  • 2,375
  • 3
  • 17
  • 25
-1

You can just use addChangeHandler() and onChange, tb.getValue().

Churro
  • 4,166
  • 3
  • 25
  • 26
  • No, this has the same problem as above. It is only called when the element loses focus. – Nathaniel Johnson Oct 05 '13 at 19:08
  • 1
    Sorry, I misunderstood your question. As @enrybo said, `KeyUpHandler` will have to do. If you look in `SuggestBox`, they request suggestions immediately with a `KeyUpHandler` – Churro Oct 05 '13 at 19:29
  • @Emmentaler I looked into the GWT event system. According to the method `com.google.gwt.user.client.impl.DOMImpl.eventGetTypeInt(String)`, the GWT handler that corresponds to the native javascript `change` event is `ChangeHandler`, as determined by `com.google.gwt.user.client.ui.Widget.onBrowserEvent(Event)`. – Churro Oct 05 '13 at 20:28
  • `input` is the event I need bound https://developer.mozilla.org/en-US/docs/Web/Reference/Events/input – Nathaniel Johnson Oct 05 '13 at 20:56