3

I am creating a custom widget, say "CustomWid" in UiBinder. And in CustomWid.java file I am writing two constructors one with zero args like CustomWid(){....}

 another with some args like 

CustomWid(String a,String b){......} So,Now I am using my custom widget in another .ui.xml file,in that .ui.xml file it is working fine when we give <my:CustomWid/> alone, and also fine when we give like <my:CustomWid a="srt1" b="str2"/> alone But "MY PROBLEM" is whenever I am trying to give both the tags in the one .ui.xml as <my:CustomWid/> <my:CustomWid a="str1" b="str2"/> Now it is throwing error when i am using both types of tags in a single .ui.xml I mean How to use my custom widget tag like a prdefined tag? I am using @uiConstructor, but it showing error Please developers... I need answer as early as possible

Azeez
  • 318
  • 3
  • 14

2 Answers2

4

UiBinder will only ever use a single constructor for a given widget: either its zero-arg constructor, or a @UiConstructor (I'm surprised that you say it works when using either one or the other call but not both: one should fail in every case, and one should succeed in every case; if you haven't annotated a constructor with @UiConstructor, then <my:CustomWid/> should always work and <my:CustomWid a="str1" b="str2"/> should always fail)

There are two solutions here:

  • use setters for the a and b attributes (void setA(String a) and void setB(String b))), and possibly check later (say, in onLoad or onAttach) that you have either none or both of A and B, but not one without the other (if that's your rule).
  • use @UiField(provided = true) when you need to use the other constructor (if you choose to have UiBinder use the zero-arg constructor –i.e. no @UiConstructor–, then that means you'll have to move the a="str1" b="str2" from the XML to the Java code: @UiField(provided = true) CustomWid myCustomWid = new CustomWid("str1", "str2")).

The first option has my preference.

Thomas Broyer
  • 64,353
  • 7
  • 91
  • 164
  • So, if you are correct..then How the GWT team is making widgets like and also it is possible to include in the same file.So How they are acheving it.....Please Explain If you are familiar with this concept – Azeez Jul 19 '13 at 10:08
  • `CellPanel`s (`HorizontalPanel` and `VerticalPanel`) have a special _parser_. There's however no extension point to make your own parser for your widgets. – Thomas Broyer Jul 19 '13 at 10:19
  • Note: I added `@UiField(provided = true)` as an alternative in my answer, for completeness; I think you already knew about it. – Thomas Broyer Jul 19 '13 at 10:23
0

It Will not show any errors...'

  @UiConstructor
  public Component(String displayText,String heading)
      {
 initWidget(uiBinder.createAndBindUi(this));

  this.displayText.setText(displayText);
    this.heading.setText(heading);  
    }`

now use another constructor with default parameters also it will work public Component() { initWidget(uiBinder.createAndBindUi(this)); }
now if you add with xml parameters component and without parameters also works in the same page.

HariKishore K
  • 399
  • 3
  • 6