ScalaFX Button => How to define the action?
Asked Answered
J

4

7

I'm trying to definde the onAction accion for a Button done in scalafx but I can't make it to work.

package App.Desktop

import javafx.event.EventHandler

import scalafx.event.ActionEvent

import scalafx.scene.control.Button

class Window() {

  btn_YES.onAction = (event: ActionEvent) => 
   new EventHandler[ActionEvent] {
     override def handle(event: ActionEvent) {
        /*Do something*/
      }
    }
  }
}

I've done this but I get an error

Error: type mismatch;
 found   : scalafx.event.ActionEvent => javafx.event.EventHandler[scalafx.event.ActionEvent]
 required: javafx.event.EventHandler[javafx.event.ActionEvent]
  btn_YES.onAction = (event: ActionEvent) => new EventHandler[ActionEvent]

I also tried to use the javafx.event.ActionEvent instead of scalafx but it doesn't work either.

Any clue?

Thanks

Jugulate answered 12/4, 2016 at 10:44 Comment(0)
M
3

I'm not a Scala programmer, but it looks like you are mixing two different forms here: a lambda expression and an explicit class.

Try

package App.Desktop

import javafx.event.EventHandler

import javafx.event.ActionEvent

import scalafx.scene.control.Button

class Window() {

    btn_YES.onAction = 
        new EventHandler[ActionEvent] {
            override def handle(event: ActionEvent) {
                /*Do something*/
            }
        }

}

or

package App.Desktop

import javafx.event.EventHandler

import javafx.event.ActionEvent

import scalafx.scene.control.Button

class Window() {

  btn_YES.onAction = (event: ActionEvent) =>  {
        /*Do something*/
  }

}
Moria answered 12/4, 2016 at 12:59 Comment(1)
Good answer, but you should also add import scalafx.Includes._ - it will add some implicit conversions, which I think is necessary for the second version (with a function rather than an EventHandler). Generally speaking, this import should always be used in ScalaFX code.Economizer
R
4

Fist, working with ScalaFX it is important to import scalafx.Includes._. It brings in many ScalaFX features.

There are two recomended ways to add onAction handler. The main one is to use (event:ActionEvent) => { ... ) closure:

import scalafx.Includes._
import scalafx.event.ActionEvent

btn_YES.onAction = (event: ActionEvent) =>  {
  /*Do something*/
}

If you do not care about event object. You can save some typing and use handle {...}:

import scalafx.Includes._

btn_YES.onAction = handle {
  /*Do something*/
}

In both cases you need to import scalafx.Includes._

Reify answered 25/4, 2016 at 15:46 Comment(0)
M
3

I'm not a Scala programmer, but it looks like you are mixing two different forms here: a lambda expression and an explicit class.

Try

package App.Desktop

import javafx.event.EventHandler

import javafx.event.ActionEvent

import scalafx.scene.control.Button

class Window() {

    btn_YES.onAction = 
        new EventHandler[ActionEvent] {
            override def handle(event: ActionEvent) {
                /*Do something*/
            }
        }

}

or

package App.Desktop

import javafx.event.EventHandler

import javafx.event.ActionEvent

import scalafx.scene.control.Button

class Window() {

  btn_YES.onAction = (event: ActionEvent) =>  {
        /*Do something*/
  }

}
Moria answered 12/4, 2016 at 12:59 Comment(1)
Good answer, but you should also add import scalafx.Includes._ - it will add some implicit conversions, which I think is necessary for the second version (with a function rather than an EventHandler). Generally speaking, this import should always be used in ScalaFX code.Economizer
B
0

You can use this for button action,

btn_YES.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
            //Do some action here
    }
});
Brasserie answered 12/4, 2016 at 12:24 Comment(0)
D
0

I am tried

Import scalafx.Includes._
import scalafx.event.ActionEvent

btn_YES.onAction = (event: ActionEvent) =>  {
  /*Do something*/
}
...

import scalafx.Includes._

btn_YES.onAction = handle {
  /*Do something*/
}

on javafx.scene.SubScene.setOnKeyPressed (..), setOnMouseEntered(..) it definitely does NOT WORK.

new EventHandler[KeyEvent]() {
      override def handle(event:KeyEvent){...}
}

that work for me.

Dulcia answered 22/1, 2021 at 12:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.