In GWT, How to use custom widget tag in an .ui.xml file with and without parameters for the tag in the same file

StackOverflow https://stackoverflow.com/questions/17742020

  •  03-06-2022
  •  | 
  •  

Question

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

Était-ce utile?

La solution

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.

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top