Discussions
Categories
- 196.9K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.3K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 545 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.9K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.6K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 440 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
Lambda expressions

OTG-467455
Member Posts: 506 Bronze Badge
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.
Answers
-
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.