Search code examples
gwtuibinder

Switching UI:Binder Widgets at runtime


I'd like to have a UI:Binder generated page where I can switch part of the UI at runtime. Here's an example:

//...More UI Binder stuff
<g:Widget ui:field="generalWidget" />
//...More UI Binder stuff

Now in my view implementation, I want to change the exact widget type that generalWidget shows. For example, I'd like to be able to do something like this:

@UiBinder Widget generalWidget;
...
if (<condition>) 
   generalWidget = new TabLayoutPanel(...);
else
   generalWidget = new FlowPanel(...);

However this doesn't work for me. When I hit the page, I get:

java.lang.AssertionError: This UIObject's element is not set; you may be missing a call to either Composite.initWidget() or UIObject.setElement()

in the Dev log. As far as I can tell, this is due to me using g:widget, because if I use the panels directly it works.

What's the best way to resolve this issue and get the switching widget behaviour I want?


Solution

  • You're missing the provided = true on the @UiField:

    @UiField(provided = true) Widget generalWidget;
    

    And of course make sure you assign that field before calling createAndBindUi!