DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Using GWT Widgets in Vaadin 7 - Part 2

Using GWT Widgets in Vaadin 7 - Part 2

Nicolas Fränkel user avatar by
Nicolas Fränkel
CORE ·
Jun. 12, 12 · Java Zone · Interview
Like (0)
Save
Tweet
5.79K Views

Join the DZone community and get the full member experience.

Join For Free

In part 1 of Using GWT widgets, we've seen how to wrap a Vaadin component around a GWT widget so that we're able to manipulate on server-side components. For the time being, we are unable to configure the widget though. We're lacking a vital part in Vaadin-GWT teaming: the shared state.

Shared state is exactly what it means, a placeholder for both server component and client widget to share the same information. In our Bootstrap example, it will enable us to set the text shown in our brand-new BootstrapButton instances. Of course, other attributes can be configured by following the same path.

First thing first, we have to create a BootstrapButtonState class extending ComponentState. That class has to be located in the same package as the connector (see part 1 for a quick refresher on the connector if the need be). The next step is to add the relevant property.

public class BootstrapButtonState extends ComponentState {

    private String text = "";

    public String getText() {

        return text;
    }

    public void setText(String text) {

        this.text = text;
    }
}

Since connectors are listeners of change state events, we now need to override its onStateChanged() method.

public void onStateChanged(StateChangeEvent stateChangeEvent) {

    super.onStateChanged(stateChangeEvent);

    BootstrapButtonState state = getState();

    VBootstrapButton button = getWidget();

    button.setText(state.getText());
}

Notice we also have overriden getWidget() and getState() to return the adequate type for our Bootstrap button (code not shown).

The final step is to bridge the server component to the shared state.

public class BootstrapButton extends AbstractComponent {

    public BootstrapButton(String caption) {

        setText(caption);
        setImmediate(true); // It's a button!
    }

    @Override
    public BootstrapButtonState getState() {

        return (BootstrapButtonState) super.getState();
    }

    public String getText() {

        return getState().getText();
    }

    public void setText(String text) {

        getState().setText(text);

        requestRepaint();
    }
}

Note the requestRepaint() is mandatory to mark the state as changed.

That's it: we have managed to change the button's text from the server code! From this point on, we can also configure both style and size to be able to mix and match between all attributes. Sources for this article can be found on GitHub.

In the final part of this serie, we'll look at how to send client events to the server.

Vaadin

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Test JavaScript Code in a Browser
  • ETL, ELT, and Reverse ETL
  • Debugging Java Collections Framework Issues in Production
  • Create a Self-Service Customer Support Chatbot Without Code

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo