Java Reference
In-Depth Information
Solution
Utilize a lambda expression to incorporate the action functionality using compact, in-
line syntax. The following JavaFX application contains a button that utilizes a lambda
expression to encapsulate the functionality of the button, rather than an anonymous in-
ner class.
public class Recipe_02_01 extends Application {
final Group root = new Group();
/**
* @param args the command-line arguments
*/
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello Lambda");
Scene scene = new Scene(root, 300, 250);
Button btn = new Button();
Label message = new Label();
btn.setLayoutX(60);
btn.setLayoutY(80);
btn.setText("Invoke Lambda Expression");
btn.setOnAction((event) -> {
message.setText("Lambda expression
invoked!");
});
root.getChildren().add(btn);
message.setLayoutX(300/2 - 90);
message.setLayoutY(30);
root.getChildren().add(message);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Search WWH ::




Custom Search