To handle Button click event using Lambda in JavaFX
// Handle the action events for the OK button. ok.setOnAction(e->{ response.setText("OK Pressed"); });
Full source
// Use an image with a button. import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ContentDisplay; import javafx.scene.control.Label; import javafx.scene.image.ImageView; import javafx.scene.layout.FlowPane; import javafx.stage.Stage; public class Main extends Application { private Label response; public static void main(String[] args) { launch(args);/*from w w w. j a v a 2 s .co m*/ } public void start(Stage myStage) { myStage.setTitle("Use Images with Buttons"); FlowPane rootNode = new FlowPane(10, 10); rootNode.setAlignment(Pos.CENTER); Scene myScene = new Scene(rootNode, 250, 450); myStage.setScene(myScene); // Create a label. response = new Label("Push a Button"); // Create buttons. Button ok = new Button("OK"); Button cancel = new Button("Cancel"); // Handle the action events for the OK button. ok.setOnAction(e->{ response.setText("OK Pressed"); }); // Handle the action events for the cancel button. cancel.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent ae) { response.setText("Cancel Pressed"); } }); // Add the label and buttons to the scene graph. rootNode.getChildren().addAll(ok, cancel, response); myStage.show(); } }