开发者

GWT: uiBinder-based widget cant be instanced second time

I created a widget using GWT uiBinder. It works fine, till the moment when I want to instance it second time. After i call constructor second time it returns only raw description from XML and statements in constructor (rootElement.add( new HTML( "panel1" ), leftId );) are just don't work. It throws no error or warning.

Please help

Java class:

public class DashboardLayout extends Composite {

final String leftId = "boxLeft";
final String rightId = "boxRight";

interface DashboardLayoutUiBinder extends UiBinder<HTMLPanel, DashboardLayout> {
}

private static DashboardLayoutUiBinder ourUiBinder = GWT.create( DashboardLayoutUiBinder.class );

@UiField
HTMLPanel htmlPanel;

public Dash开发者_如何学JAVAboardLayout() {
    HTMLPanel rootElement = ourUiBinder.createAndBindUi( this );
    this.initWidget( rootElement );

    rootElement.add( new HTML( "panel1" ), leftId );
    rootElement.add( new HTML( "panel2" ), rightId );

}
   }

XML descriprion:

<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
             xmlns:g='urn:import:com.google.gwt.user.client.ui'
             >
    <g:HTMLPanel ui:field="htmlPanel">
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td width="40%" id="boxLeft" class="boxContextLeft">

                </td>

                <td width="60%" id="boxRight" class="boxContextRight">

                </td>
            </tr>
        </table>
    </g:HTMLPanel>
</ui:UiBinder>


Don't use id="myid" in widgets, as they will be global(which will screw you up) instead of scoped per instantiation of the widget; use ui:field="myid" and then create a corresponding UiField variable in the java class. This will allow the gwt compiler to obfuscate the id's so you don't get collisions between instantiations of the same widget.

DashboardLayout.java

public class DashboardLayout extends Composite {

    interface DashboardLayoutUiBinder extends
            UiBinder<HTMLPanel, DashboardLayout> {
    }

    private static DashboardLayoutUiBinder ourUiBinder = GWT
            .create(DashboardLayoutUiBinder.class);

    @UiField
    HTMLPanel htmlPanel;

    @UiField
    HTML panel1;

    @UiField
    HTML panel2;

    public DashboardLayout() {
        HTMLPanel rootElement = ourUiBinder.createAndBindUi(this);
        this.initWidget(rootElement);

        // do stuff with panel1
        panel1.setHTML("<blink>blink</blink>");

        // do stuff with panel2
        panel2.setHTML("<marquee>marquee</marquee>");
    }
}

DashboardLayout.ui.xml

<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
    xmlns:g='urn:import:com.google.gwt.user.client.ui'>
    <g:HTMLPanel ui:field="htmlPanel">
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td width="40%" class="boxContextLeft">
                    <g:HTML ui:field="panel1"></g:HTML>
                </td>

                <td width="60%" class="boxContextRight">
                    <g:HTML ui:field="panel2"></g:HTML>
                </td>
            </tr>
        </table>
    </g:HTMLPanel>
</ui:UiBinder>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜