ScalaFx: Event Handler with First Class Function
Asked Answered
U

1

9

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.

Untune answered 17/2, 2014 at 17:59 Comment(0)
P
20

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:

Petrozavodsk answered 18/2, 2014 at 2:37 Comment(1)
I have resolved my 4-5 error by searching and finding this same answer again & again. Thanks for reminding me to include scalafx.IncludesAffective

© 2022 - 2024 — McMap. All rights reserved.