You are viewing documentation for Vaadin Framework 7 and related products

Using Vaadin with Scala · Vaadin
Vaadin Framework - Creating a Vaadin Project - Using Vaadin with Scala
 Edit This Page

Using Vaadin with Scala

You can use Vaadin with any JVM compatible language, such as Scala or Groovy. There are, however, some caveats related to libraries and project set-up. In the following, we give instructions for creating a Scala UI in Eclipse, with the Scala IDE for Eclipse and the Vaadin Plugin for Eclipse.

  1. Install the Scala IDE for Eclipse, either from an Eclipse update site or as a bundled Eclipse distribution.

  2. Open an existing Vaadin Java project or create a new one as outlined in Creating and Running a Project with Eclipse. You can delete the UI class created by the wizard.

  3. Switch to the Scala perspective by clicking the perspective in the upper-right corner of the Eclipse window.

  4. Right-click on the project folder in Project Explorer and select Configure ▸ Add Scala Nature.

  5. The web application needs scala-library.jar in its class path. If using Scala IDE, you can copy it from somewhere under your Eclipse installation to the class path of the web application, that is, either to the WebContent/WEB-INF/lib folder in the project or to the library path of the application server. If copying outside Eclipse to a project, refresh the project by selecting it and pressing F5.

    You could also get it with an Ivy or Maven dependency, just make sure that the version is same as what the Scala IDE uses.

You should now be able to create a Scala UI class, such as the following:

class MyScalaUI extends UI {
  override def init(request: VaadinRequest) = {
    val content: VerticalLayout = new VerticalLayout

    val label: Label = new Label("Hello, world!")
    content addComponent label

    // Handle user interaction
    content addComponent new Button("Click Me!",
      new ClickListener {
        override def buttonClick(event: ClickEvent) =
"The time is " + new Date)

Eclipse and Scala IDE should be able to import the Vaadin classes automatically when you press Ctrl+Shift+O.

You need to define the Scala UI class either in a servlet class (in Servlet 3.0 project) or in a web.xml deployment descriptor, just like described in Exploring the Project for Java UIs.

The Scaladin add-on offers a more Scala-like API for Vaadin. A Vaadin 7 compatible version is under development.

Defining Listeners with Lambda Expressions

Scala does not support use of lambda expressions for calling functional interfaces, like Java 8 does. Hence, we can’t just use a lambda expression for the ClickListener in the example above. You can, however, define implicit conversions from lambda expressions to such interface implementations. For example, for click listeners:

implicit def clickListener(f: ClickEvent => Unit) =
  new ClickListener {
    override def buttonClick(event: ClickEvent) {

You could then use a lambda expression as follows:

content addComponent new Button("Click Me!",
  (event: ClickEvent) =>"The time is " + new Date))