Call click () as programmatically in GWT

I want to call the click event function for a button in GWT ... I tried this code, but it does not work.

Button btnAddField = new Button();
btnAddField.setText("Add");
btnAddField.setWidth("225px");
btnAddField.addClickHandler(new btnAddFieldButtonClickListener());  


private class btnAddFieldButtonClickListener implements ClickHandler{   
        public void onClick(ClickEvent event) {
Window.alert("Called Click Event");
}
}

this function will call a button but it is not called when this function is called btnAddField.click()

+5
source share
2 answers

I solve this problem using this code

btnAddField.fireEvent(new ButtonClickEvent ())

private class ButtonClickEvent extends ClickEvent{
        /*To call click() function for Programmatic equivalent of the user clicking the button.*/
    }

Now it works great.

+2
source

You can also try:

view.btnAddField.fireEvent(new ClickEvent() { } );

(A little hack because it com.google.gwt.event.dom.client.ClickEventhas a protected constructor.)

or

DomEvent.fireNativeEvent(Document.get().createClickEvent(0, 0, 0, 0, 0,
            false, false, false, false), view.btnAddField);

Then, in both cases, there is no need to create separate classes and break encapsulation for handlers in order to test click events.

+6
source

All Articles