scala - Compile error trying to bind mouse event listener in ScalaFx -


i'm coding little test application uses canvas class in scala scalafx. problem with:

gui.canvas.handleevent(mouseevent.mouseclicked){      a: mouseevent => {           println("mouse pos:" + a.scenex+ "  " + a.sceney)      }       } 

throwing error:

[error] e:\dev\sg\src\main\scala\test.scala:27: type mismatch; [error]  found   : scalafx.scene.input.mouseevent => unit [error]  required: _2.handlermagnet[javafx.scene.input.mouseevent,?]  val _2: scalafx.scene.canvas.canvas [error]       a: mouseevent => { [error]                     ^ 

i don't why exact same snippet compiles when appears in method setupgraphics not in when it's in main object's code.

btw, having browsed quite bit of scalafx code, i'm getting bit dizzy because i've seen lots different ways bind event, there's the 1 i'm using, setting onmouseclicked variable (with , without first class function), using filterevent. also, each can use anonymous function or create event handler object.

of those, 1 worked in example; trying create eventhandler[mouseevent] , override handle() fails in case.

i'm using scala 2.10.5

full source:

object test {   import scalafx.includes._   import scalafx.application.jfxapp   import scalafx.application.jfxapp.primarystage   import scalafx.stage.stage    import scalafx.scene.scene   import scalafx.scene.canvas.canvas   import scalafx.scene.canvas.graphicscontext    import scalafx.scene.input.mouseevent   import scalafx.event.eventhandler      class gui(val canvas:canvas     ,val stage: stage     ,val gc: graphicscontext){}    object main extends jfxapp {      var gui=setupgraphics()      gui.canvas.handleevent(mouseevent.mouseclicked){       a: mouseevent => {         println("mouse pos:" + a.scenex+ "  " + a.sceney)       }     }      def setupgraphics():gui={       val canvas = new canvas(400, 400)       canvas.translatex=0       canvas.translatey=0       val gc = canvas.graphicscontext2d        canvas.handleevent(mouseevent.mouseclicked){         a: mouseevent => {           println("mouse pos:" + a.scenex+ "  " + a.sceney)         }       }        stage = new primarystage {         title = "asd"         height = 600         width = 800         scene=new scene{content=canvas}       }        return new gui(canvas,stage,gc)     }       }     } 

this looks me bug in scala compiler, can correct using intermediate variable canvas without modifying else. instead of calling gui.canvas.handleevent, first assign canvas variable call handleevent on that:

var gui=setupgraphics() val canvas = gui.canvas canvas.handleevent(mouseevent.mouseclicked){   a: mouseevent => {     println("mouse pos:" + a.scenex+ "  " + a.sceney)   } } 

Comments