views:

118

answers:

2

Hello all,

I'm trying to use uiBinder. I followed the tutorial provided by google, but I don't know why clickevent doesn't work? I want to count number of clicks and show it in the span, it doesn't work, I also put window.alert but it seems that the event handler is not called at all! Can anyone help me? It's couple of hours I'm working on it but can't find the problem!

Thank you so much

P.S. Below is my code


<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
   xmlns:g="urn:import:com.google.gwt.user.client.ui">
   <ui:style>
   </ui:style>
   <g:HTMLPanel>
    <table>
        <tr>
            <td><img ui:field='imgPrd'/></td>
            <td>
               <span ui:field='lblNum'></span>
                <g:Button ui:field='btnAdd'></g:Button>
            </td>
        </tr>
    </table>
   </g:HTMLPanel>


public class uiProductList extends Composite {

@UiField Button btnAdd;
@UiField ImageElement imgPrd;
@UiField SpanElement lblNum;

int count;
private static uiProductListUiBinder uiBinder =
GWT.create(uiProductListUiBinder.class);

interface uiProductListUiBinder extends UiBinder<Widget,
uiProductList> {
}

public uiProductList() {
   initWidget(uiBinder.createAndBindUi(this));
}


@UiHandler("btnAdd")
void handleClick(ClickEvent e) {
  Window.alert("test");
  count++;       
  lblNum.setInnerText(Integer.toString(count));
 }

}
A: 

@Jason: it doesn't work, I add in the constructor I'm not sure if it's the right place. @Igor: yes I closed it just it was missed when I copied it here. And I'm using NetBean 6.9 and gwt 2.0.3, if it helps

public uiProductList() {
    initWidget(uiBinder.createAndBindUi(this));
     btnAdd.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            Window.alert("test");
        }
    });
}

And that's the way I call this class in the entity point class

public void onModuleLoad() {

    uiProductList uiProduct = new uiProductList();
    RootPanel.getBodyElement().appendChild(uiProduct.getElement());}
Marjan
+2  A: 

You should correctly add your widget to the root panel. Use

RootPanel.get().add(uiProduct);

Otherwise the handlers are not initialized.

Hilbrand