Search code examples
scalafx

ScalaFx MouseEvent: java.lang.NoSuchFieldError: BACK


What can be the reason for getting this error in run-time (see title) while referring to ScalaFx class, instead if I switch to JavaFx class reference (workaround) things work as expected? With Scala 2.12 and ScalaFx 8.0.192-R14 things were working without JavaFx based workaround. About the environment: Scala 2.13.1, ScalaFx 12.0.2-R18, IntelliJ 2019.3.2, Java 8, Windows 10. Below I am providing the core snippets hopefully able to highlight the issue.

With ScalaFx MouseEvent class reference it seems to generate the exception with me.button:

import scalafx.Includes._    
import scalafx.scene.input.{MouseButton, MouseEvent}
...
    def flowPaneEvents(flowpane: FlowPane): Unit = {
      flowpane.onMouseClicked = (me: MouseEvent) => {
        // this statement causes the exception with scalafx
        me.button match {
          case MouseButton.Primary   => println("primary button")
          case MouseButton.Secondary => println("secondary button")
          case _ =>
        }
        me.consume()
      }
    }

Whereas referring to javaFx classes things are working fine. See below:

import scalafx.Includes._    
import javafx.scene.{input => jfxsi}
...
def flowPaneEvents(flowpane: FlowPane): Unit = {
  flowpane.onMouseClicked = (me: MouseEvent) => {
    // this javafx based reference gets things done
    me.getButton match {
      case jfxsi.MouseButton.PRIMARY => println("primary button")
      case jfxsi.MouseButton.SECONDARY => println("secondary button")
      case _ =>
    }
    me.consume()
  }
}

What am I missing (I've tried to re-import sbt library-dependencies, but I've not been lucky so far)?


Solution

  • ScalaFX 12.0.2 is to be used with JavaFX 12. If you are using it with Java 8 you will run into strange issues when you have JavaFX 8 is in the path. Use ScalaFX 8 for Java 8. This is clearly stated on the project website: https://github.com/scalafx/scalafx#scalafx-8

    Field "BACK" was added in JavaFX 12. See API documentation here: https://openjfx.io/javadoc/12/javafx.graphics/javafx/scene/input/MouseButton.html#BACK It is not present in JavaFX 8, so that is the reason for "java.lang.NoSuchFieldError: BACK" - ScalaFX is trying to access field that is not present.