According to: http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#lambda-expressions-in-gui-applications
Previously:
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
Now, we can:
btn.setOnAction(
event -> System.out.println("Hello World!")
);
Now, I try to do that in Scala when using a Java library.
I'm using JavaFX (which is included by default in Java 1.8 SE). Try:
chart.setOnMouseClicked( (e: MouseEvent) => println("Noice") )
However, I get:
Error:(204, 46) type mismatch;
found : javafx.scene.input.MouseEvent => Unit
required: javafx.event.EventHandler[_ >: javafx.scene.input.MouseEvent]
chart.setOnMouseClicked( (e: MouseEvent) => println("Noice") )
^
The old style works fine:
chart.setOnMouseClicked( new EventHandler[MouseEvent] {
override def handle(event: MouseEvent): Unit = println("NOT NOICE")
} )
I set the project language level to Java 8 in IntelliJ, I'm using Scala 2.11.1, and Java from Oracle version 1.8.0_05
What am I missing here? or is it simply not possible to pass a lambda expression from Scala to Java the same way it is done in the mentioned example?