Over a million developers have joined DZone.
Platinum Partner

Using GWT widgets in Vaadin 7 - Part 1

· Java Zone

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.

Even if Vaadin provides you with plenty of components out-of-the-box, chances are sooner or later, you'll want to use that special GWT widget you just saw the last day. In that case, be happy because Vaadin let you do that. Then, you'll be able to package the component in a simple JAR archive and reuse it in all projects you want, just like Vaadin components themselves. In this 3 parts article series, we'll see how to do just that with Vaadin 7:

  • This first part is about just wrapping GWT widgets in Vaadin components
  • In the second part, we'll see how to configure the client widget from the server component
  • The final part will detail how to communicate from client to server

 The first step it to choose which widget to wrap. As an illustration, we'll use GWT-Bootstrap, a GWT implementation of Twitter Bootstrap and most specifically the com.github.gwtbootstrap.client.ui.Button. Of course, lessons learned here can easily be transposed for other GWT widgets.

Wrapping a GWT widget in Vaadin code requires a couple of classes:

  • The first class to create is the Vaadin component itself. Such components should extends com.vaadin.ui.AbstractComponent (or com.vaadin.ui.AbstractComponentContainer for components that contains other components, which is not the case for buttons):
    public class BootstrapButton extends AbstractComponent {
  • The second class to create is the client widget itself. It's very simple: just extend the wanted GWT widget (the Bootstrap button in our case). Note that it's mandatory to locate it in a ui.client subpackage relative to the above component:
    public class VBootstrapButton extends Button {
        public VBootstrapButton() {
  • The final class will bind the component and the widget. In Vaadin semantics, it's called a connector. Connectors have to extend com.vaadin.terminal.gwt.client.ui.AbstractComponentConnector (or com.vaadin.terminal.gwt.client.ui.AbstractComponentContainerConnector for those widgets that contain other widgets). Connecting is achieved by implement the createWidget() that should return the previously implemented client class and by annotating the connector with the @Connect that takes the server class as the value.
    public class BootstrapButtonConnector extends AbstractComponentConnector {
        protected Widget createWidget() {
            return GWT.create(VBootstrapButton.class);
    It also has to be located in the ui.client subpackage.

GWT reads Java code and renders HTML and JavaScript. When using standard Vaadin components, the code is precompiled and available in the Vaadin JAR, but when adding third-party widgets, we need to compile both Vaadin and the other widgets. In order to achieve this, two things are necessary:

  • First, we have to create a GWT widgetset gwt.xml file, referencing both the Vaadin and the Bootstrap widgetset:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 1.7.0//EN" "http://google-web-toolkit.googlecode.com/svn/tags/1.7.0/distro-source/core/src/gwt-module.dtd">
        <inherits name="com.vaadin.terminal.gwt.DefaultWidgetSet" />
        <inherits name="com.github.gwtbootstrap.Bootstrap" />
        <!-- Reduces compilation time in development mode -->
            <set-property name="user.agent" value="safari,gecko1_8" />
  • Then, we have to handle the compilation itself. Either uses the Eclipse Vaadin plugin compilation feature or insert the following snippet in the Maven POM:
                    <extraJvmArgs>-Xmx512M -Xss1024k</extraJvmArgs>

 Sources for this article can be found on GitHub. In the next part, we'll detail how to let developers customize widgets though components on the server side.

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}