Creating a Component Using Existing Components

    • Div

      • Label

    One option would be to create the component by extending Div but that would expose all Div API like add(Component) to the user. To avoid this, the component can be based on a :

    A Composite will automatically create the root component, specified using generics (Composite<Div>) and make it available through getContent(). In the constructor you only need to create the other components and add them to the root Div. The value is set using setValue in the Input component.

    To make the component easier to use, you can add an API for getting and setting the value and the label text by delegating to the Input and Label components:

    Java

    1. public String getValue() {
    2. return input.getValue();
    3. }
    4. input.setValue(value);
    5. }
    6. public String getLabel() {
    7. return label.getText();
    8. }
    9. public void setLabel(String labelText) {
    10. label.setText(labelText);
    11. }

    The public API of TextField only contains the defined methods in addition to the few generic methods defined in the interface.

    Tip
    Using Components instead of Elements do not introduce overhead in the browser.