MoonBoots89
MoonBoots89

Reputation: 357

GWT UIBinder method return type void?

Bellow is some Java code from Google's UIBinder tutorial. Together with a separate HTML page this code displays the text "Hello, World".

public class HelloWorld {
    interface MyUiBinder extends UiBinder<DivElement, HelloWorld> {}
    private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

    @UiField SpanElement nameSpan;

    public HelloWorld() {
        setElement(uiBinder.createAndBindUi(this));
    }

    public void setName(String name) { 
        nameSpan.setInnerText(name); 
    }

    /**
    * Method in question
    */
    public void Element getElement() { 
        return nameSpan; 
    }
}

The getElement() method has a void return type but returns an Element called nameSpan. How is this possible given that it has a void return type?

Upvotes: 0

Views: 136

Answers (1)

Chris Lercher
Chris Lercher

Reputation: 37778

The explanation is simply, that the example in the documentation is "a little bit" broken.

The implementation of setElement() and getElement() would be unnecessary anyway, if the example simply extended UIObject like

public class HelloWorld extends UIObject {

  private static HelloWorldUiBinder uiBinder = 
        GWT.create(HelloWorldUiBinder.class);

  interface HelloWorldUiBinder extends UiBinder<Element, HelloWorld> {
  }

  @UiField
  SpanElement nameSpan;

  public HelloWorld() {
    setElement(uiBinder.createAndBindUi(this));
  }

  public void setName(String name) { 
    nameSpan.setInnerText(name); 
  }

}

By the way, here's a standalone variation of the UiBinder "hello world" example (which might be easier to understand as a first UiBinder example):

public class HelloWorld implements EntryPoint {

  interface HelloWorldUiBinder extends UiBinder<Element, HelloWorld> {
  }

  @UiField SpanElement nameSpan;

  public void onModuleLoad() {
    final HelloWorldUiBinder uiBinder = GWT.create(HelloWorldUiBinder.class);
    final Element element = uiBinder.createAndBindUi(this);
    nameSpan.setInnerText("world");
    Document.get().getBody().appendChild(element);
  }
}

Upvotes: 1

Related Questions