GWT Compsite's initWidget not applicable for DivElement

607 Views Asked by At

I'm implementing MVP in my first GWT app using the recommended API from their docs, and admittedly, am doing some cargo cult programming while scrambling to learn/understand the API:

public class DefaultSignInView extends Composite implements SignInView {
    private static DefaultSignInViewUiBinder uiBinder = GWT
        .create(DefaultSignInViewUiBinder.class);

    public DefaultSignInView() {
        // ERROR: The method initWidget(Widget) in the type Composite is not applicable
        // for the arguments (DivElement)
        initWidget(uiBinder.createAndBindUi(this));
    }

    // Extends the UiBinder interface for this particular view.
    interface DefaultSignInViewUiBinder extends UiBinder<DivElement, DefaultSignInView> {
        // No-op.
    }
}

In the above code snippet, the call to initWidget is a syntax/compiler error:

The method initWidget(Widget) in the type Composite is not applicable for the arguments (DivElement).

Now in the documentation (linked above) they show that you should extend UiBinder without the generic arguments. But I've also seen examples that use the generics, and again, cargo culting it, I borrowed from another example that used DivElement as the first argument. So, a few questions:

  1. Why am I getting this syntax error?
  2. What can I change DivElement to (or what else would I have to change) to correct it, besides removing the generic arguments? And if the generic arguments are deprecated or are truly no longer used, can someone explain why? In that case I'll just @SuppressWarnings.
  3. Would someone provide a clear, layman's explanation of what the code is doing here? I hate cargo culting my code.
1

There are 1 best solutions below

0
On

I sincerely think you are looking in the wrong place. Try this here. Most of it is explained in the docs, still i will try to highlight a few points,

  1. What you are trying to do is very simple example of a UiBinder template that contains no widgets, only HTML.
  2. So not being a widget, you cannot add it to a panel as a widget, instead you need to do a direct DOM manipulation.

I am adding a my sample code below

public class ImageViewer implements EntryPoint
{

    @Override
    public void onModuleLoad()
    {
    SampleUI sampleUI = new SampleUI();
    sampleUI.setNameSpan(" Trying it out!!");

    Document.get().getBody().appendChild(sampleUI.getElement());
    }
}

SampleUI ui-binder

<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder">
<ui:style>
    .important {
        font-weight: bold;
    }
</ui:style>
<div>
    Hello,
    <span class="{style.important}" ui:field="nameSpan" />
</div>
</ui:UiBinder>

SampleUI java

public class SampleUI extends UIObject
{

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

    interface SampleUIUiBinder extends UiBinder<DivElement, SampleUI>
    {
    }

    private DivElement  root;

    @UiField
    SpanElement         nameSpan;

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

    public DivElement getRoot()
    {
        return root;
    }

    public void setNameSpan(String firstName)
    {
        nameSpan.setInnerText(firstName);
    }
}