Search code examples
gwttextboxonfocus

GWT onFocus event not getting triggered on text box


onFocus event is not getting triggered on the text box. I have below code, i am using GWT 2.6.0

When i used the focus handler on text box then its working properly but i also have on browser event where i am not able to trigger focus event.Below is the code for on browser.

@Override
    public void onBrowserEvent( Context context, Element parent, String value, NativeEvent event,
            ValueUpdater<String> valueUpdater )
    {
        String eventType = event.getType();     

      if( BrowserEvents.FOCUS.equals( eventType ) )
        {
           //Do what you want on focus
        }

Solution

  • You should use the methods provided (for your ease) by GWT:

    textBox.addFocusHandler(new FocusHandler() {
        @Override
        public void onFocus(FocusEvent event) {
            //Do some operation onFocus event
        }
    });
    

    It's part of the HasFocusHandlers which is implemented by quite a few of GWT's widgets. Similar handlers can be found for mouse, keyboard, blur, etc. events.

    For completeness sake: if you need to listen directly on the DOM ONFOCUS event on any Widget, you need to call the sinkEvents method first with the appropriate bits/flags (or override it in your widget). You can find the relevant flags in the Event class.