Your First ScalaFX App

Create src/main/scala/HelloWorld.scala:

import scalafx.application.JFXApp3
import scalafx.scene.Scene
import scalafx.scene.control.Label
import scalafx.scene.layout.StackPane

object HelloWorld extends JFXApp3 {
  override def start(): Unit = {
    stage = new JFXApp3.PrimaryStage {
      title = "Hello, ScalaFX"
      width = 400
      height = 300
      scene = new Scene {
        root = new StackPane {
          children = new Label("Hello, ScalaFX!")
        }
      }
    }
  }
}

04-hello-world.png.png

Run it with sbt run. A 400×300 window titled "Hello, ScalaFX" should appear with the text centered in it.

Walking through the code

A note on threading

Creation or modification of JavaFX/ScalaFX UI elements must run on the JavaFX Application Thread. start() is already called on that thread, so code inside it can safely construct the app. More complex applications may need to run longer operations. To prevent the UI from locking up, longer-running operations (network calls, long computations) need to run outside the JavaFX Application Thread. You can ensure that code is running outside the JavaFX Application Thread using Platform.runLater. We cover this later in the Concurrency section.