如何使用uibinder为儿童创建一个gwt复合组件? [英] How to create a gwt composite component with children using uibinder?
本文介绍了如何使用uibinder为儿童创建一个gwt复合组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
mycomponent.ui.xml:
$ b $我想创建一个组件来装饰它的子元素,例如: b
< g:FlowPanel addStyleNames =myStyle>
<! - 我如何呈现孩子? - >
< / g:FlowPanel>
然后其他人可以使用:
< MYAPP:myComponent的>
< g:标签>装饰的孩子< / g:标签>
< / myapp:mycomponent>
我如何渲染uibinder中的孩子? (或在Java中,如果我必须的话)
解决方案
$ b
让 MyComponent
用于添加/删除子窗口小部件的 HasWidgets
接口。
$ b
MyComponent.ui.xml
看起来就像
< g:< g:FlowPanel ui:field =main/>
当您委托指定的方法ind HasWidgets
到 FlowPanel
:
public class MyComponent extends Composite implements HasWidgets {
private static MyComponentUiBinder uiBinder = GWT.create(MyComponentUiBinder.class);
接口MyComponentUiBinder扩展了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>
< / M:MyComponent>
将以这种方式工作。
I would like to create a component to decorate its children, such as:
mycomponent.ui.xml:
<g:FlowPanel addStyleNames="myStyle">
<!-- how can i render children ? -->
</g:FlowPanel>
and then others can use:
<myapp:mycomponent>
<g:Label>Decorated child</g:Label>
</myapp:mycomponent>
How can i render the children in uibinder? (or in Java, if i must)
解决方案
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.
这篇关于如何使用uibinder为儿童创建一个gwt复合组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文