Is it possible to use a Java 8 style method references in Scala?

后端 未结 3 1149
小蘑菇
小蘑菇 2020-12-05 18:12

I\'m developing a JavaFX8 application in Scala but I couldn\'t figure out how to pass a method reference to an event handler. To clarify, I\'m not using ScalaFX library but

3条回答
  •  Happy的楠姐
    2020-12-05 18:33

    You should pass function which applying one parameter of type ActionEvent:

    val button = new Button()
    val inputController = new InputController()
    
    def handler(h: (ActionEvent => Unit)): EventHandler[ActionEvent] =
      new EventHandler[ActionEvent] {
        override def handle(event: ActionEvent): Unit = h(event)
      }
    
    button.setOnAction(handler(inputController.handleFileSelection))
    

提交回复
热议问题