Kandha
Kandha

Reputation: 3689

call click() function as programmatically in GWT

i want to call click event function for Button in GWT... I tried this code but it is not working..

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 wiil call at click the button but it does not call when call this function btnAddField.click()

Upvotes: 5

Views: 15262

Answers (2)

omnomnom
omnomnom

Reputation: 9139

You can also try:

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

(It's a little hack, because com.google.gwt.event.dom.client.ClickEvent has protected constructor.)

or

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

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

Upvotes: 6

Kandha
Kandha

Reputation: 3689

I solve that problem by 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.*/
    }

It is working fine now.

Upvotes: 2

Related Questions