Source of ButtonDemoLambda.java


  1: import javafx.application.Application;
  2: import javafx.scene.Scene;
  3: import javafx.stage.Stage;
  4: import javafx.scene.layout.VBox;
  5: import javafx.scene.control.Button;
  6: import javafx.event.ActionEvent;
  7: import javafx.event.EventHandler;
  8: import javafx.scene.control.Label;
  9: 
 10: /**
 11: Event handling with lambda functions.
 12: */
 13: public class ButtonDemoLambda extends Application
 14: {
 15:    public static void main(String[] args)
 16:    {
 17:       launch(args);
 18:    }
 19: 
 20:    @Override
 21:    public void start(Stage primaryStage) throws Exception
 22:    {
 23:           VBox root = new VBox();
 24:       Button btnSunny;
 25:       Button btnCloudy;
 26:       Label lblMessage;
 27:           btnSunny = new Button("Sunny");
 28:           btnCloudy = new Button("Cloudy");
 29:           lblMessage = new Label("Click a button.");
 30: 
 31:           btnSunny.setOnAction(e ->
 32:             {
 33:                            lblMessage.setText("It is sunny!");
 34:                    }
 35:           );
 36:           btnCloudy.setOnAction(e ->
 37:             {
 38:                            lblMessage.setText("It is cloudy!");
 39:                    }
 40:           );
 41: 
 42:           root.getChildren().add(btnSunny);
 43:           root.getChildren().add(btnCloudy);
 44:           root.getChildren().add(lblMessage);
 45: 
 46:              Scene scene = new Scene(root, 300, 100);
 47:       primaryStage.setTitle("Lambda Button Demo");
 48:       primaryStage.setScene(scene);
 49:       primaryStage.show();
 50:    }
 51: }