UiBinder helps developers to build GWT widgets in a few simple steps, exploiting the flexibility and maintainability of XML. This allows developer without a strong background in java to be competitive in the GWT code production.
Thus we see UiBinder in action : first of all we must create a web application that use Google Web Toolkit. To do this, I use the eclipse's plugin for GWT, so from the prompt do :
File --> New --> Other --> Google --> Web application project. Then fill the required fields, uncheck the "Use Google app engine" option and click "Finish".
Now we realize the following html page containing a simple widget :

Initially we have to modify the home page, an HTML page, in which we create a "div" element that permit us to inject content from a Java class, the entryPoint :
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet" href="PostBlogUiBinder.css">
<title>Simple widget project</title>
<script type="text/javascript" language="javascript" src="postbloguibinder/postbloguibinder.nocache.js"></script>
</head>
<body>
<!-- OPTIONAL: include this if you want history support -->
<iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
<!-- RECOMMENDED if your web app will not function without JavaScript enabled -->
<noscript>
<div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif">
Your web browser must have JavaScript enabled
in order for this application to display correctly.
</div>
</noscript>
<h1>Sample Widget</h1>
<h3 align="center">Fill the following fields</h3>
<div id="simpleWidget" align="center"></div>
</body>
</html>
as you can see we have create a div element with id called "simpleWidget" that we refer from the entrypoint java class.
Now we have to inject into this page the widget that we want to create. We do this into the entrypoint class, called SimpleWidgetEntrypoint :
import com.post.uibinder.ui.SimpleWidget;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
/**
* Entry point classes define onModuleLoad().
*/
public class SimpleWidgetEntrypoint implements EntryPoint {
/**
* This is the entry point method.
*/
public void onModuleLoad() {
SimpleWidget simpleWidget = new SimpleWidget();
RootPanel.get("simpleWidget").add(simpleWidget);
}
}
Now we have to create our widget, we can do this with the wizard plugin doing the following actions :
from the eclipse's prompt do File --> New --> Other --> Google Web Toolkit --> UiBinder --> Next
then digit the name of the java class of the widget, in our case "SimpleWidget" and then click Finish.
We can see now that the wizard has created two files :
- SimpleWidget.java
- SimpleWidget.ui.xml
To obtain our result widget we have to modify both files :
- SimpleWidget.ui.xml
<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:g="urn:import:com.google.gwt.user.client.ui">
<ui:style>
.important {
font-weight: bold;
}
</ui:style>
<g:HTMLPanel ui:field="mainPanel">
<g:VerticalPanel>
<g:Label ui:field="descriptionLabel" ui:text="Put a description of yourself"></g:Label>
<g:TextArea ui:field="textArea" ui:text="Put here your description"></g:TextArea>
<g:Label ui:field="banLabel" ui:text="Select which band you prefer among these"></g:Label>
<g:RadioButton ui:name="band" ui:text="U2" ></g:RadioButton>
<g:RadioButton ui:name="band" ui:text="Radiohead"></g:RadioButton>
<g:RadioButton ui:name="band" ui:text="Pearl Jam"></g:RadioButton>
<g:RadioButton ui:name="band" ui:text="Depeche mode"></g:RadioButton>
</g:VerticalPanel>
<g:Button styleName="{style.important}" ui:field="button" ui:text="Confirm" />
</g:HTMLPanel>
</ui:UiBinder>
- SimpleWidget.java
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;
public class SimpleWidget extends Composite {
private static SimpleWidgetUiBinder uiBinder = GWT
.create(SimpleWidgetUiBinder.class);
interface SimpleWidgetUiBinder extends UiBinder {
}
@UiField
Button button;
@UiField
TextArea textArea;
public SimpleWidget() {
initWidget(uiBinder.createAndBindUi(this));
textArea.setSize("300px", "200px");
}
@UiHandler("button")
void onClick(ClickEvent e) {
Window.alert("OK");
}
}
To build the widget we need the following GWT elements :
- VerticalPanel
- Label
- RadioButton
- Button
- TextArea
if we don't use the UiBinder utilities we have to write the entire widget only with java code, but this time into the SimpleWidget java class we have only specified the size of the text area, all the other elements with their values are specified into the XML file.
This is why UiBinder simplifies the construction of GWT widget and simplifies also the collaboration between UI designers, which are more confortable with markup languages, and developers.