I've recently started using GWT in one of my applications. I had a doubt related to the usage of UiBinder. If I've declared a @UiField attribute in my class (and similarly in .ui.xml as well), can I create a new object of that type and assign it to the same reference, after I've created it once in the constructor?
For example, if my ui.xml file has a DockLayoutPanel which has a PlotWidget in its center.
public class PlotWidget extends Composite {
@UiField (provided = true)
SimplePlot plot;
public Constructor() {
plot = new SimplePlot(someArgument1, someArgument2);
}
// some method
public doSomething() {
// Is this valid?
plot = new SimplePlot(someArgument3, someArgument4);
}
}
Does the plot remain attached to the DockLayoutPanel or not? If not, how would I achieve the functionality where I need to create new objects like above?
Any pointers will be appreciated.
What's missing from your example code is the ui binder create call, something like initWidget(uiBinder.createAndBindUi(this));
. This is called after the initialization of, in this case, the ui field plotContainer
in the constructor. If you would reassign a new instance to SimplePlot
the new instance won't be attached to the DockLayoutPanel and if you did replace the existing instance somehow you also need to reattach the handlers.
A solution could be to or instead of creating a new SimplePlot instance set the arguments via method calls on SimplePlot. However, if the only way to create SimplePlot is through the constructor, if you have no control over the implementation, you could create a separate uibinder class/widget for creating SimplePlot that creates SimplePlot and attaches handlers and set that new widget via a setWidget
on the PlotWidget as sinicyn describes.