views:

84

answers:

1

For example I took html from a designer which is given below. How can i add click event which shows alert from GWT?

final Button button = new Button("OK"); I dont allow to add button dynamically from GWT by RootPanel.get("sendButtonContainer").add(button);

I am searching the syntax something like: RootPanel.get("sendButtonContainer").getWidget(0).addEventListener()??????? Jquery can search the button object from nameFieldContainer and dynamically add click event listener but is it possible in GWT??

+1  A: 

You're looking for ClickHandler. Listeners were deprecated some time ago.

button.addClickHandler(new ClickHandler() {
  @Override
  public void onClick(ClickEvent event) {
    // do something...
  }
}

edit: I may have misunderstood your question. If you no longer have a reference to the button you could do

// code removed because it's really not the best way to solve this problem, and led to confusion...

But you'll have to be very sure that the element inherits from HasClickHandlers -- and you should really just keep a reference to the button, it's much cleaner that way.

Jason Hall
I have a <div id="test"><input type="button" value="OK" /></div>html tag. is it possible in this case for your solution
msaif
I executed but no action. I used ((HasClickHandlers)RootPanel.get("test").getWidget(0)).addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { Window.alert('sss'); }}
msaif
I noticed you asked another question about this, you really should have just waited for a followup. You should just add the button in GWT and keep a reference to it to add the handler.
Jason Hall