Search code examples
javascripteventsgwttextboxdom-events

Enabling and Disabling Save button on a GWT page


The application I am working on has pages of setup data with lots of textboxes; each page having a save button. I have defined most of the pages as a *.ui.xml file and used the GWT 2.0 UI binding.

I would like to add a save button which is disabled onload and is only enabled after a user modifies the data into one of the textboxes.

I know I can register an event handler against each element on the page to enable the button, but I wanted a more elegant solution. Ideally, I would like to define a button that "listens" to events on the page and changes itself. Is this possible in GWT?


Solution

  • You can also add the change event to your whole containing widget using addDomHandler

    public class SetupDataPage extends Composite{
    
      // Binder stuff  
    
      // Button from the binder
      @UiField Button saveBtn;
    
      public SetupDataPage(){
            addDomHandler(new ChangeHandler() {
    
                @Override
                public void onChange(ChangeEvent event) {
                    saveBtn.setEnabled(true);
                }
            }, ChangeEvent.getType());
      }
    }
    

    Beware This will trigger the event for all textboxes in SetupDataPage. On the other side it might not work for some specific widgets. I know it works for GWT's textbox, checkbox, listbox...