Monthly Archives: May 2013

Getting Started with ScalaFX: Compile and Run

ScalaFX is a Scala library for creation user interface applications based on JavaFX platform. Its intention is to be simpler to use and more “Scala-like” compared to the underlying JavaFX API.

This post is intended to help you setup a basic Scala project that is using ScalaFX library. The setup only requires dependencies on the ScalaFX, JavaFX, and the Scala libraries. If you add those three libraries you are good to go and develop ScalaFX application.

We will cover steps to do the setup using SBT build script. SBT is a build tool for Scala and Java projects.  SBT is de-facto standard build system for many Scala projects. Even if you do not use it, it is good to be familiar with it. You can also use an SBT build script for creating Eclipse, IDEA, NetBeans, and Maven setups (covered in a future post).

In our example we will compile and run a simple ScalaFX demo:

package hello

import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.geometry.Insets
import scalafx.scene.Scene
import scalafx.scene.control.Label
import scalafx.scene.layout.BorderPane

object HelloSBT extends JFXApp {
  stage = new PrimaryStage {
    scene = new Scene {
      root = new BorderPane {
        padding = Insets(25)
        center = new Label("Hello SBT")
      }
    }
  }
}

The demo opens a window and displays text “Hello SBT” in the center:

hello-sbt

Prerequisites

As the minimum starting requirement you only need to install Java SDK and SBT. Everything else needed to compile and run ScalaFX applications will be downloaded by SBT automatically.

You will also need a text editor to edit. Any decent test editor will do, for instance, jEdit or Sublime Text.

1. Install Java 7

I assume that you have Java 7 installed on your system. If you do not, you can download latest version from Oracle’s Java SE page.

2. Set JAVA_HOME environment variable

JavaFX is distributed with recent Java 7 releases, but it is not automatically added to class path. You need to define environment variable JAVA_HOME pointing to the installation directory of the JDK. This variable will be used by SBT script to locate JavaFX binaries: jfxrt.jar.

On Windows the installation directory will be something like:

  C:\Program Files\Java\jdk1.7.0_21

on Linux it could be:

  /usr/lib/jvm/default-java

3. Install SBT

If you do not have SBT on your system install it following steps on “SBT Setup” page. To test the installation open command prompt and type (without “>”):

  > sbt sbt-version

If SBT is installed properly, it should respond with a series of printouts. If the is the first time you run SBT it will complete installation by downloading some dependencies needed by SBT. At the end SBT should print version information, for instance:

  ...
  [info] 0.12.3

Building and Running ScalaFX Apps using SBT

SBT can be used to easily build and run Scala and Java. SBT assumes default organization of files in you project:

  src/
    main/
      resources/
        <files to include in main jar here>
      scala
        <main Scala sources>
      java/
        <main Java sources>
    test/
      resources
        <files to include in test jar here>
      scala/
        <test Scala sources>
      java/
        <test Java sources>

If you are familiar with Maven, the SBT directory structure is the same. For instance, in your project directory Scala sources located in subdirectory src/main/scala.

1. Create “hello-sbt” project directory

Let’s first create a directory for our sample projects, call it, for instance, hello-sbt. We will add content as we progress. At the beginning our project directory is empty.

2. Create SBT configuration file

In the newly create directory create new file called build.sbt with the following content:

// Name of the project
name := "Hello SBT"

// Project version
version := "1.0"

// Version of Scala used by the project
scalaVersion := "2.10.1"

// Add dependency on ScalaFX library
libraryDependencies += "org.scalafx" %% "scalafx" % "1.0.0-M3"

// Add dependency on JavaFX library based on JAVA_HOME variable
unmanagedJars in Compile += Attributed.blank(file(System.getenv("JAVA_HOME") + "/jre/lib/jfxrt.jar"))

Note that each declaration is separated by a blank line. This is required by SBT. The first two declarations define our projects name and version. The third one specifies which version of Scala we want to use. The forth one, libraryDependencies += …, defines managed dependency on the ScalaFX library. Managed dependencies are automatically downloaded by SBT from Maven Central. The last declaration “unmanagedJars in Compile” instructs SBT where to look for JavaFX library. It is unmanaged meaning that SBT will not attempt to download it on its own. We make use of the JAVA_HOME environment variable defined earlier to point to location of JavaFX runtime: jfxrt.jar.

3. Create simple ScalaFX application

By default, SBT looks for Scala source files in subdirectory src/main/scala of you project. For our example we will use package name hello. Inside our project directory, next to build.sbt create nested directories src/main/scala/hello.

Inside inside src/main/scala/hello create HelloSBT.scala:

package hello

import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.geometry.Insets
import scalafx.scene.Scene
import scalafx.scene.control.Label
import scalafx.scene.layout.BorderPane

object HelloSBT extends JFXApp {
  stage = new PrimaryStage {
    scene = new Scene {
      root = new BorderPane {
        padding = Insets(25)
        center = new Label("Hello SBT")
      }
    }
  }
}

4. Run your application using SBT

At this point you should have everything needed to build and run the sample application. Open command prompt and change directory to the hello-sbt project (where build.sbt is located).

  > sbt run

If everything is installed and configured fine, SBT will compile the code and run your code. The SBT output will look something like:

  The system cannot find the file C:\Apps\sbt\bin\sbtconfig.txt.
  [info] Updating ...  
  [info] Resolving ...
  ...
  [info] Done updating.
  [info] Set current project to Hello SBT (…)
  [info] Updating {file:…}...
  [info] Resolving org.scala-lang#scala-library;2.10.1 ...
  [info] Resolving org.scalafx#scalafx_2.10;1.0.0-M3 ...
  [info] Done updating.
  [info] Compiling 1 Scala source to …/hello-sbt/target/scala-2.10/classes...
  [info] Running hello.HelloSBT

hello-sbt

Congratulations! Now you know how to compile and run ScalaFX code using SBT.

Closing Thoughts

You can follow steps in this post to create your own SBT project file for ScalaFX. SBT is well documented with an easy to follow “Getting Started” guide.

To learn more about ScalaFX visit the home page, check links and user groups there. The best ways to learn ScalaFX is to learn JavaFX first. There are numerous JavaFX tutorials on the Oracle website. I recommend getting a copy of the “Pro JavaFX 2” book, one of the coauthors is creator of ScalaFX Steve Chin. ScalaFX versions of the examples from the “Pro JavaFX 2” book are on GitHub project ProScalaFX.

The source for this post is in GitHub ScalaFX-Tutorials project under hello-sbt.

Advertisements