【问题标题】:How to create a gwt composite component with children using uibinder?如何使用 uibinder 创建带有子级的 gwt 复合组件?
【发布时间】:2011-05-09 16:20:09
【问题描述】:

我想创建一个组件来装饰它的孩子,例如:

mycomponent.ui.xml:

<g:FlowPanel addStyleNames="myStyle">
    <!-- how can i render children ? -->
</g:FlowPanel>

然后其他人可以使用:

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

如何在 uibinder 中渲染孩子? (或者在 Java 中,如果我必须的话)

【问题讨论】:

    标签: java gwt uibinder


    【解决方案1】:

    MyComponent 实现HasWidgets 接口,用于添加/删除子小部件。

    MyComponent.ui.xml 看起来很简单

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

    当您将 ind HasWidgets 指定的方法委托给 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);
        }
    }
    

    打电话

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

    会以这种方式工作。

    【讨论】:

    • 非常感谢您提供非常完整的答案。
    • 我一直在寻找一种简单的方法来做到这一点。谢谢!
    • 不注入 MyComponentUiBinder 而不是 GWT.create 是否会影响性能?
    • AFAIK 注入使用的是 GWT.create 所以我会说不
    【解决方案2】:

    使用这个 XML:

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

    将实例化MyComponent,然后调用MyComponent.add(label)。您所要做的就是在您的类MyComponent 中覆盖.add(..) 并应用您想要传递组件的任何样式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-08
      相关资源
      最近更新 更多