ScalaFx: Event Handler with First Class Function

喜夏-厌秋 提交于 2019-12-23 07:38:51

问题


i try to write an event handler in a scalaFx app. I found followin solution:

import scalafx.scene.control.ListView
import javafx.scene.input.MouseEvent
import javafx.event.EventHandler

...

    val list = new ListView[String] {
        onMouseClicked = new EventHandler[MouseEvent] {
            override def handle(event: MouseEvent) {
                doSomething(event)
            }
        }
    }

But this seems to be very Java-style boilerplate code. Is there a way to do this with a first class function like this?

        onMouseClicked = (event: MouseEvent) => doSomething(event)

Compiler says:

No implicit view available from javafx.scene.input.MouseEvent => scalafx.delegate.SFXDelegate[javafx.scene.input.MouseEvent] with javafx.scene.input.MouseEvent.


回答1:


When you work with ScalaFX you always need to remember to use:

import scalafx.Includes._

This will resolve the error you have. There are many examples of using event handlers the ScalaFX way, for instance here:

  • Event handling examples in ProScalaFX
  • Event handling demos in ScalaFX-Demos
  • Event filter examples


来源:https://stackoverflow.com/questions/21836133/scalafx-event-handler-with-first-class-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!