123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- ---
- title: Using Vaadin with Scala
- order: 1000
- layout: page
- ---
-
- [[getting-started.scala]]
- = 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.
-
- . Install the link:http://scala-ide.org/[Scala IDE for Eclipse], either from an
- Eclipse update site or as a bundled Eclipse distribution.
-
- . Open an existing Vaadin Java project or create a new one as outlined in
- <<dummy/../../../framework/getting-started/getting-started-first-project#getting-started.first-project,"Creating
- and Running a Project with Eclipse">>. You can delete the UI class created by
- the wizard.
-
- . Switch to the Scala perspective by clicking the perspective in the upper-right
- corner of the Eclipse window.
-
- . Right-click on the project folder in [guilabel]#Project Explorer# and select
- "Configure > Add Scala Nature".
-
- . The web application needs [filename]#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
- [filename]#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 kbd:[F5].
-
- +
- You could also get it with a 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:
-
-
- [source, scala]
- ----
- @Theme("mytheme")
- class MyScalaUI extends UI {
- override def init(request: VaadinRequest) = {
- val content: VerticalLayout = new VerticalLayout
- setContent(content)
-
- 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) =
- Notification.show("The time is " + new Date)
- })
- }
- }
- ----
-
- Eclipse and Scala IDE should be able to import the Vaadin classes automatically
- when you press kbd:[Ctrl+Shift+O].
-
- You need to define the Scala UI class either in a servlet class (in Servlet 3.0
- project) or in a [filename]#web.xml# deployment descriptor, just like described
- in
- <<dummy/../../../framework/getting-started/getting-started-first-project#getting-started.first-project.exploring,"Exploring
- the Project">> for Java UIs.
-
- ifdef::web[]
- The link:https://github.com/henrikerola/scaladin[Scaladin add-on] offers a more
- Scala-like API for Vaadin. A Vaadin 7 compatible version is under development.
- endif::web[]
-
- ifdef::web[]
- [[getting-started.scala.lambdas]]
- == 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 [interfacename]#ClickListener# in the example above. You can, however,
- define implicit conversions from lambda expressions to such interface
- implementations. For example, for click listeners:
-
-
- [source, scala]
- ----
- implicit def clickListener(f: ClickEvent => Unit) =
- new ClickListener {
- override def buttonClick(event: ClickEvent) {
- f(event)
- }
- }
- ----
-
- You could then use a lambda expression as follows:
-
-
- [source, scala]
- ----
- content addComponent new Button("Click Me!",
- (event: ClickEvent) =>
- Notification.show("The time is " + new Date))
- ----
-
- endif::web[]
|