Creating a simple component

To make a component with a new client-side widget (as opposed to making a server-side composite), you will need to make three things: the server-side component you’ll actually use in your application (let’s call it MyComponent), the corresponding client-side (GWT) widget that will render your component in the browser (MyComponentWidget) and a Connector that handles the communication between the two (MyComponentConnector). (Note that although MyComponentWidget could in principle be a Connector as well, in practice it’s a good idea to separate the two.)

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

Java

  1. package com.example.mycomponent;
  2. import com.vaadin.ui.AbstractComponent;
  3. public class MyComponent extends AbstractComponent {
  4. }

The main thing to notice here is that it inherits AbstractComponent, 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. setStyleName(CLASSNAME);
  8. }
  9. }

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:

Java

  1. package com.example.mycomponent.client;
  2. import com.example.mycomponent.MyComponent;
  3. import com.google.gwt.core.client.GWT;
  4. import com.google.gwt.user.client.ui.Widget;
  5. import com.vaadin.client.ui.AbstractComponentConnector;
  6. import com.vaadin.client.ui.Connect;
  7. @Connect(com.example.mycomponent.MyComponent.class)
  8. public class MyComponentConnector extends AbstractComponentConnector {
  9. @Override
  10. protected Widget createWidget() {
  11. return GWT.create(MyComponentWidget.class);
  12. }
  13. }

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

    • MyComponentWidgetset.gwt.xml

  • 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

  1. @Widgetset("com.example.mycomponent.MyComponentWidgetset")
  2. class MyUI extends UI {

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

XML

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

Add MyComponent to your application, and it should render a label saying “This is MyComponent”.

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