At this point the basic MyComponent has no functionality except inherited basic component features (we’ll add functionality in following articles):

    Java

    The main thing to notice here is that it inherits , which is the most common case (unless it will contain other components, see separate article about component containers). The component will automatically have the basic component features, such as size and caption.

    At this point our basic client-side widget will just statically render some text:

    Java

    1. package com.example.mycomponent.client;
    2. import com.google.gwt.user.client.ui.Label;
    3. public class MyComponentWidget extends Label {
    4. public static final String CLASSNAME = "mycomponent";
    5. public MyComponentWidget() {
    6. setText("This is MyComponent");
    7. }

    Notice that this is actually a plain GWT widget that can be used as any other GWT widget. It’s a good idea to set a style name from the start, so that the component can be styled.

    Now all we have to do is connect the component to the widget using a Connector:

    The crucial Connect annotation is what actually tells the framework what is connected where - do this first, since it’s easy to forget.

    In createWidget() use GWT.create() instead of new whenever possible, since it allows for some flexibility that might come in handy later on.

    Though this is optional, you might also want to override getWidget() so that you can narrow it’s return type from Widget to your actual implementation class:

    Java

    1. @Override
    2. public MyComponentWidget getWidget() {
    3. return (MyComponentWidget) super.getWidget();
    4. }

    The package structure usually looks something like this:

    • com.example.mycomponent

      • MyComponent.java

    • com.example.mycomponent.client

      • MyComponentConnector.java

      • MyComponentWidget.java

    Finally, compile the widgetset, and make sure the widgetset is defined with the @Widgetset annotation in the UI class:

    Java

    If you are using web.xml, it should contain the widgetset parameter:

    XML

    1. <servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
    2. <init-param>
    3. <description>Vaadin UI</description>
    4. <param-name>UI</param-name>
    5. <param-value>com.example.myexampleproject.MyApplicationUI</param-value>
    6. </init-param>
    7. <init-param>
    8. <param-name>widgetset</param-name>
    9. <param-value>com.example.mycomponent.MyComponentWidgetset</param-value>
    10. </init-param>

    Next have a look at the articles covering shared state and RPC, to learn how to add more functionality to the component.