gwt custom widget with child elements configuration in UIBinder (like CustomButton)
Asked Answered
C

1

13

I've faced with a task to create custom widget, it has container behavior -- 3 panels inside. And i would like to use it in general UIBinder way like CustomButton

 <u:MyWidget>
   <u:image><g:Image .../></u:image>
   <u:mainContent><g:Panel.../></u:mainContent>
 </u:MyWinget>

Is it possible to define that custom child elements somehow or maybe it is predefined thing in the UIBuilder?

Thanks in advance

Cothurnus answered 4/12, 2011 at 13:4 Comment(0)
I
16

Correct way to do it is to use the UiChild annotation.

public class MyWidget extends Composite {
  public MyWidget() {
    // ...
  }

  @UiChild( tagname = "image" )
  void addImage(Image image) {
    // ...
  } 

  @UiChild( tagname = "mainContent" )
  void addMainContent(Widget contentWidget) {
  }
}
Iiette answered 5/12, 2011 at 4:26 Comment(2)
@Thomas - But how do you do this generically? For example if all child panels implemented myInnerPanel, how could I access that child? The docs say that this tag name must exactly match the method name. Is there a work around?Buenrostro
okay, got it working. As long as my generic type, say MyAbstractWidget is extended by the child of the 'customtag' tag, then in the @ UiChild method, I can upcast from Widget to MyAbstractWidget without issue. That way the @ UiChild method and parent widget can handle any properly formed future child of 'customtag.'Buenrostro

© 2022 - 2024 — McMap. All rights reserved.