Christian Achilli
Christian Achilli

Reputation: 5707

GWT UiBinder: onClick event does not fire

Hallo folks,

I have a widget defined as follow:

<g:HTMLPanel>
    <g:HTML ui:field="rootElement" styleName="{resources.mainStructure.widgetBox}"/>
</g:HTMLPanel>

and in the related java class there's something like this:

@UiField HTML rootElement; 

for(int row = 0; row < 10; row ++) {            
    rootElement.getElement().appendChild(new MyWidget().getElement());
}

where MyWidget is something like this other:

<g:HTMLPanel>

<table  border="0" cellpadding="0" cellspacing="0" class="{resources.mainStructure.areaWidget}"> 
     <tbody class="{resources.mainStructure.workArea}"> 
       <tr>
         <td ui:field="td_1"/> 
         <td ui:field="td_2"/> 
         <td ui:field="td_3"/> 
         <td ui:field="td_4"/> 
         <td ui:field="td_5"/> 
         <td ui:field="td_6"/> 
         <td ui:field="td_7"/> 
         <td ui:field="td_8"/> 
       </tr> 
     </tbody> 
     <tfoot class="{resources.mainStructure.lineaBus}"> 
       <tr> 
         <th ui:field="th_1"/> 
         <th ui:field="th_2"/> 
         <th ui:field="th_3"/> 
         <th ui:field="th_4"/> 
         <th ui:field="th_5"/> 
         <th ui:field="th_6"/> 
         <th ui:field="th_7"/> 
         <th ui:field="th_8"/> 
       </tr> 
     </tfoot> 
 </table> 

At some point I attach to a certain td another widget, this one:

<g:HTMLPanel>

    <g:Anchor styleName="{resources.mainStructure.orangeButton}" ui:field="selectMe">+</g:Anchor>

</g:HTMLPanel>

Now, the problem is that when I click the anchor of the above widget, no click is fired. If I attach the anchor widget to another widget,not to the td one, it works fine. So far I understand the problem is in the first widget, because if I attach MyWidget to nothing than the "selectMe" onclick event is fired. Any comment? Thanks, Chris

Upvotes: 0

Views: 2605

Answers (2)

Peter Wagener
Peter Wagener

Reputation: 2092

Be careful using calls like...

rootElement.getElement().appendChild(new MyWidget().getElement());

The 'appendChild(...)' method is nothing more than a basic DOM attachment; it doesn't do any of the work for event propagation. Is there a reason you're not using the Widget add method()?

rootElement.add(new MyWidget());

That should work, and will line up all the events you need.

Upvotes: 3

Emil
Emil

Reputation: 113

This worked for me, not sure how you structured it. So as I understood it you wan't the output to be sort of like this:

<g:HTMLPanel>
    ...
         <td ui:field="td_1">
            <g:HTMLPanel>
                <g:Anchor styleName="" ui:field="selectMe">+</g:Anchor>
            </g:HTMLPanel>
         </td> 
    ...

In the ui binder class where I created the selectMe anchor I just added an UiHandler like following:

@UiField Anchor selectMe;
@UiHandler("selectMe")
void onSelectMeAnchorClick(ClickEvent event) {
    Window.alert("clicked on selectMe");
}

This triggered the the alert for me when I click on the link. Hope this could help you.

Upvotes: 0

Related Questions