Replacing Anonymous Inner Classes - Java Lambda Stream

Java examples for Lambda Stream:Lambda

Introduction

Replace the anonymous inner classes with lambda expressions.

btn.setOnAction(e -> {
  System.out.println("Player added.");
  System.out.println("== Current Player List==");
});
    
    

Demo Code

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application {

  public static void main(String[] args) {
    launch(args);/*  w w w.  j av a2 s .  c  o m*/
  }

  @Override
  public void start(Stage primaryStage) {
    primaryStage.setTitle("Team Entry Form");
    // Creating a GridPane container
    GridPane grid = new GridPane();
    grid.setPadding(new Insets(10, 10, 10, 10));
    grid.setVgap(5);
    grid.setHgap(5);

    Button btn = new Button();
    btn.setText("Enter Player");

    btn.setOnAction(e -> {
      System.out.println("Player added.");
      System.out.println("== Current Player List==");
    });

    GridPane.setConstraints(btn, 0, 5);
    grid.getChildren().add(btn);

    StackPane root = new StackPane();
    root.getChildren().add(grid);

    primaryStage.setScene(new Scene(root, 300, 250));
    primaryStage.show();
  }
}

Related Tutorials