How to create a gwt composite component with children using uibinder?

一个人想着一个人 提交于 2019-12-03 01:47:16

Let MyComponent implement the HasWidgets interface for adding/removing child widgets.

The MyComponent.ui.xml looks as simple as

<g:FlowPanel ui:field="main" />

while you delegate the methods specified ind HasWidgets to the FlowPanel:

public class MyComponent extends Composite implements HasWidgets {

    private static MyComponentUiBinder uiBinder = GWT.create(MyComponentUiBinder.class);

    interface MyComponentUiBinder extends UiBinder<Widget, MyComponent> {}

    @UiField
    FlowPanel main;

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

    @Override
    public void add(Widget w) {
        main.add(w);
    }

    @Override
    public void clear() {
        main.clear();
    }

    @Override
    public Iterator<Widget> iterator() {
        return main.iterator();
    } 

    @Override
    public boolean remove(Widget w) {
        return main.remove(w);
    }
}

Calling

<M:MyComponent>
    <g:Label text="some text" />
</M:MyComponent>

will work this way.

Using this XML:

<myapp:mycomponent>
    <g:Label>Decorated child</g:Label> 
</myapp:mycomponent>

will instantiate MyComponent and then call MyComponent.add(label). All you have to do is override .add(..) in your class MyComponent and apply any styles that you want to passed components.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!