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:

scala

  1. @Theme("mytheme")
  2. class MyScalaUI extends UI {
  3. override def init(request: VaadinRequest) = {
  4. val content: VerticalLayout = new VerticalLayout
  5. setContent(content)
  6. val label: Label = new Label("Hello, world!")
  7. content addComponent label
  8. // Handle user interaction
  9. content addComponent new Button("Click Me!",
  10. new ClickListener {
  11. override def buttonClick(event: ClickEvent) =
  12. Notification.show("The time is " + new Date)
  13. })
  14. }
  15. }

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:

scala

  1. implicit def clickListener(f: ClickEvent => Unit) =
  2. new ClickListener {
  3. override def buttonClick(event: ClickEvent) {
  4. f(event)
  5. }
  6. }

You could then use a lambda expression as follows:

scala

  1. content addComponent new Button("Click Me!",
  2. (event: ClickEvent) =>
  3. Notification.show("The time is " + new Date))