Han Fastolfe
Han Fastolfe

Reputation: 405

GWT uibinder composite

I'm creating a composite uibinder widget with a Label and a TextBox.

The intented use is:

<x:XTextBox ui:field="fieldName" label="a caption" >
    The text to be put in the box.
</x:XTextBox>

I've found how to catch the label with a custom @UiConstructor constructor, I might add another parameter to the constructor, but I would like to know how to get the text from the xml, just like the GWT tag <g:Label>a caption</g:Label> does.

Any help is greatly appreciated.

Upvotes: 3

Views: 2740

Answers (3)

Kong
Kong

Reputation: 9556

Han is right; HasText is what you need to implement. One thing I found handy is to browse the source if you know a Google widget does something you'd like to do also. e.g.

http://www.google.com/codesearch/p?hl=en#A1edwVHBClQ/user/src/com/google/gwt/user/client/ui/Label.java

Upvotes: 0

Han Fastolfe
Han Fastolfe

Reputation: 405

I've found a possible implementation by looking at the Label widget source code.

The key point is that the composite widget must implement the HasText interface. so in the declaration and in the body:

public class XTextBox extends Composite implements HasText ...
...
@UiField TextBox textBox;
...
public void setText(String text) {
    textBox.setText(text);
}
public String getText() {
    return textBox.getText();
}
...

Upvotes: 3

aem
aem

Reputation: 3916

Just put the text into another parameter of your widget and have your @UiConstructor take that parameter. That is:

<x:XTextBox ui:field="fieldName" label="a caption" 
  text="The text to be put in the box." />

Then your XTextBox.java will have this:

@UiField TextBox textBox;

@UiConstructor XTextBox(String label, String text) {
  initWidget(uiBinder.createAndBindUi(this));
  textBox.setValue(text);
}

Upvotes: 0

Related Questions