GWT:基于uiBinder的小部件不能第二次实例化

Igo*_*nko 2 gwt uibinder

我使用GWT uiBinder创建了一个小部件.它工作正常,直到我想第二次实例化的那一刻.在我第二次调用构造函数之后,它只返回XML中的原始描述,而构造函数(rootElement.add( new HTML( "panel1" ), leftId );)中的语句只是不起作用.它不会引发任何错误或警告.

请帮忙

Java类:

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 DashboardLayout() {
    HTMLPanel rootElement = ourUiBinder.createAndBindUi( this );
    this.initWidget( rootElement );

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

}
   }
Run Code Online (Sandbox Code Playgroud)

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%" id="boxLeft" class="boxContextLeft">

                </td>

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

                </td>
            </tr>
        </table>
    </g:HTMLPanel>
</ui:UiBinder>
Run Code Online (Sandbox Code Playgroud)

ant*_*upe 7

不要id="myid"在小部件中使用,因为它们将是全局的(这会使你搞砸)而不是每个实例化小部件的范围; 使用ui:field="myid"然后在java类中创建相应的UiField变量.这将允许gwt编译器对id进行模糊处理,因此您不会在同一小部件​​的实例化之间发生冲突.

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>");
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)