public void onModuleLoad() {
/*
* Create a builder for the outermost element. The initial state of the
* builder is a started element ready for attributes (eg. "<div").
*/
DivBuilder divBuilder = ElementBuilderFactory.get().createDivBuilder();
// Add attributes to the div.
divBuilder.id("myId");
divBuilder.title("This is a div");
// Add style properties to the div.
StylesBuilder divStyle = divBuilder.style();
divStyle.trustedBackgroundColor("red");
divStyle.endStyle();
// Append a child select element to the div.
SelectBuilder selectBuilder = divBuilder.startSelect();
// Append three options to the select element.
for (int i = 0; i < 3; i++) {
OptionBuilder optionBuilder = selectBuilder.startOption();
optionBuilder.value("value" + i);
optionBuilder.text("Option " + i);
optionBuilder.endOption();
}
/*
* End the select and div elements. Note that ending the remaining elements
* before calling asElement() below is optional, but a good practice. If we
* did not call endOption() above, we would append each option element to
* the preceeding option element, which is not what we want.
*
* In general, you must pay close attention to ensure that you close
* elements correctly.
*/
selectBuilder.endSelect();
divBuilder.endDiv();
// Get the element out of the builder.
Element div = divBuilder.finish();
// Attach the element to the page.
Document.get().getBody().appendChild(div);
}