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
Room. Reactive queries
Room has great integration with LiveData. Results from SQL queries can be easily wrapped in LiveData container. This way LiveData can be observed in the UI. When the data is changed in the database, all active observers get notified and update the corresponding views.
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.
ViewModel
ViewModel – it is a model of the view. The purpose of the ViewModel is to apply any business logic to the Model before exposing it to the View for consumption. This way the View is free of business logic.