Forum Stats

  • 3,826,750 Users
  • 2,260,703 Discussions
  • 7,897,071 Comments

Discussions

Lambda expressions

OTG-467455
OTG-467455 Member Posts: 506 Bronze Badge
edited Nov 27, 2017 1:59PM in Java Puzzle Ball

Hi Nick,  This one is a bit more complex.  Also seems like the syntax is a bit different to the rest of Java.   My bedtime reading to fall asleep will be the "Java All-in-One for Dummies".  Bought it a while ago and it covers Java 8. 

OTG-467455

Answers

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited Nov 27, 2017 1:59PM

    Here is a test class to compare the way the old traditional event handling was invoked

    prior to the intro of lambda and how the lambda handles them now.

    At least, it helped me to visualize the transition from traditional call to lambda.

    import javafx.application.Application;import javafx.event.Event;import javafx.event.EventHandler;import javafx.scene.Group;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.input.MouseEvent;import javafx.stage.Stage;public class MyTest extends Application {   @Override   public void start(Stage primaryStage) {      Button btn = new Button();      btn.setText(" Click Me! ");      //1: traditional; two args to the method call      btn.addEventHandler(MouseEvent.MOUSE_CLICKED, new MyEventHandler());      //2: using generics; two args to the method call      btn.addEventHandler(         MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {            @Override            public void handle(MouseEvent evt) {               System.out.println("2. old style - inline anonymous class definition");            }         }      );      //3: converting the above #2 to lambda; two args to the method call      btn.addEventHandler(MouseEvent.MOUSE_CLICKED, evt -> {System.out.println("3. New lambda style");});      //4: old style; single arg -       btn.setOnMousePressed(new MyMousePressedHandler());      //5: converting the above #4 to lambda; single arg      btn.setOnMouseClicked(evt -> {System.out.println("4. New lambda style - Mouse info : " + evt.getButton().name());});      //6: lambda; event handler invoking a locally defined method      btn.setOnMouseExited(evt -> callSomeLocallyDefinedMethod(evt));      //Other setup and display stuff      Group root = new Group();      root.getChildren().addAll(btn);      primaryStage.setTitle("Test");      primaryStage.setScene(new Scene(root, 300, 200));      primaryStage.show();   }   private void callSomeLocallyDefinedMethod(MouseEvent e) {      System.out.println("5. Lambda style - method called by event handler.");   }         public static void main(String[] args) {      launch(args);   }}/* locally defined event handler */class MyEventHandler implements EventHandler {   public void handle(Event event) {      System.out.println("1. old style - event handler class definition");   }}/* locally defined event handler */class MyMousePressedHandler implements EventHandler {   public void handle(Event event) {      System.out.println("0. old style - event handler class definition");   }}

    EDIT: Normally, you won't see the same event is handled by many event handlers for an event source. It is shown here just for demo purposes only.

    OTG-467455OTG-467455