Getting an error that I cannot resolve symbol scalafx

Viewed 48

Beginner at scala here, been trying to import scalafx into my scala file but I just can't seem to do so.

import scalafx.application.JFXApp

object Main extends JFXApp{

}

And the sbt file that I have is

ThisBuild / version := "0.1.0-SNAPSHOT"

ThisBuild / scalaVersion := "2.13.8"

lazy val root = (project in file("."))
  .settings(
    name := "SnakeFX"
  )
libraryDependencies += "org.scalafx" %% "scalafx" % "16.0.0-R24"

Any fix for this?

1 Answers

To use ScalaFX you need to add a dependency on the ScalaFX library and also corresponding version of the JavaFX. JavaFX binaries are system dependent.

You need to tell sbt about the OS you are running, using something like:

libraryDependencies ++= {
  // Determine OS version of JavaFX binaries
  lazy val osName = System.getProperty("os.name") match {
    case n if n.startsWith("Linux") => "linux"
    case n if n.startsWith("Mac") => "mac"
    case n if n.startsWith("Windows") => "win"
    case _ => throw new Exception("Unknown platform!")
  }
  Seq("base", "controls", "fxml", "graphics", "media", "swing", "web")
    .map(m => "org.openjfx" % s"javafx-$m" % "16" classifier osName)
}

You can read the entire ScalaFX tutorial here.

Assuming you use Intellij, after you modified build.sbt, the IDE will see your changes and create a small icon on the top-right to notify you have to load the new changes into the project. The button tells your project to check the build.sbt again, and update your dependencies accordingly: either delete removed dependencies or download newly added dependencies.

enter image description here

After loading them, you should be ready to work with ScalaFX. Happy coding!

Related