Roughian Examples Site Map - GWT Examples - Tutorials

Composite Widget


Version 1.0 onwards

Abstract Wrapper For Classes You Build Yourself



Listeners


None


Notes


The Composite is just a wrapper for another widget. For example, you could wrap a VerticalPanel and add two Label widgets to it, one as a header and the other underneath to display the associated data.

The Composite widget has to know which of these other widgets it is supposed to wrap, and you tell it with the initWidget(Widget widget) method. You MUST call this once and only once, or you will get an error.

The difference between the Composite and simply extending another 'real' widget is that the Composite hides all the wrapped widget's methods and properties. For example, in the above scenario, the user of the widget wouldn't be able to add anything to the VerticalPanel.


Code


public void demo()
{
    DisplayBox displaybox = new DisplayBox("Header", "This is my data");
    RootPanel.get("demo").add(displaybox);
}
class DisplayBox extends Composite
{
    public DisplayBox(String header, String data)
    {
        VerticalPanel widget = new VerticalPanel();
        initWidget(widget);
        widget.addStyleName("demo-Composite");

        Label headerText = new Label(header);
        widget.add(headerText);
        headerText.addStyleName("demo-Composite-header");

        Label dataText = new Label(data);
        widget.add(dataText);
        dataText.addStyleName("demo-Composite-data");
    }
}