Handling Events with Lambda Expressions
Lambda Expressions and Functional Interfaces are a new feature of Java 8 and the support provided for lambda expressions is only with functional interfaces. A functional interface is an interface that contains only one abstract method. This is exactly the case with handling events in Java and more accurately for these examples for handling events using the JavaFX library. Prior to lambda expressions the code execution for a button click would go this way:
button.setOnAction(new EventHandler() {
@Override
public void handle(ActionEvent actionEvent) {
String newColor = "rgba(100, 0, 100)";
stackPane.setStyle("-fx-background-color: " + newColor);
}
});
Here is created an anonymous class that implements the EventHandler interface and it is being passed to the method setOnAction. The code that will be executed whenever the button is fired is placed within the handle method which is the only method in the EventHandler interface which makes it a functional interface. With lambda expressions the code would look like this:
button.setOnAction((ActionEvent event) -> {
String newColor = "rgba(100, 100, 0)";
stackPane.setStyle("-fx-background-color: " + newColor);
});
In this case the compiler can infer the name of the method because it is the only method in the interface so the body and the parameter declaration is enough.
Syntax
Best Practices
Lambda expressions should be simplified as much as possible. For example the event type declaration from the previous example code can be removed because the EventHandler expects an ActionEvent and the type of the event reference can also be inferred making the code like this:
button.setOnAction((event) -> {
String newColor = "rgba(100, 100, 0)";
stackPane.setStyle("-fx-background-color: " + newColor);
});
Because parentheses are not mandatory when there is one parameter passed, another improvement would be to remove them as well:
button.setOnAction(event -> {
String newColor = "rgba(100, 100, 0)";
stackPane.setStyle("-fx-background-color: " + newColor);
});
If there is only one line of code with a return statement in the method body, the return statement can also be removed.
“Effectively Final” Variables
public void start(Stage primaryStage) {
String color = "rgba(0, 100, 100)";
Trying to assign a value to color will pop-up the message: “Variable ‘color’ is already defined in the scope.”
button.setOnAction((ActionEvent event) -> {
String color = "rgba(100, 100, 0)";
stackPane.setStyle("-fx-background-color: " + color);
});
Running it will produce the error:
Error:(33,20) java: variable color is already defined in method start(javafx.stage.Stage)
Letzte Beiträge
Share :
Share :
Weitere Beiträge

MVVM’s Model implementation in Android application. Room
As we said in the previous article Model is the data in our application. These are classes representing objects that we persist in our database or that we get from network calls to services.

Machine learning concepts. Network training and evaluation
The neural network model consists of two layers – an LSTM layer and an output Dense layer. The reason for choosing an LSTM layer is the need to process sequences of time-related data

Using Room in CryptoOracle
In the Android application CryptoOracle the data is persisted in SQLite database. The reasons are two:
The application can still work and show persisted data when there is no internet connection.