• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Using Lambda Expressions of Java 8 in Java FX event handlers

May 14, 2012 //  by Mohamed Sanaulla//  Leave a Comment

Note: The Project Lambda (JSR-335) to be added in Java 8 is evolving and the sample here is how one can use Lambdas with the current Java8 build downloaded from here. I will try to update the sample if there are any changes in the API in future.

also read:

  • Java 8.0 Tutorials
  • Java 7.0 Tutorials
  • New Features in Java 7.0
  • G1 Garbage Collector in Java 7.0

I thought it will be good to get a peak of how Lambda Expressions can be used with JavaFX or for that matter any Single Abstract Method (SAM) types.

Lets build a sample with just one toggle button and change the text of the toggle as and when it is selected/un-selected.

The code with the current Java -7 version would be:

 
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ButtonBase;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleButtonBuilder;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class LambdasWithJavaFx extends Application {
  public static void main(String[] args){
    Application.launch(args);
  }
  @Override
  public void start(Stage stage) throws Exception {
    BorderPane root = new BorderPane();
    ToggleButton button = new ToggleButton("Click");

    final StringProperty btnText = button.textProperty();
    button.setOnAction(new EventHandler<ActionEvent>() {
      @Override
      public void handle(ActionEvent actionEvent) {
        ToggleButton source = (ToggleButton) actionEvent.getSource();
        if (source.isSelected()) {
          btnText.set("Clicked!");
        } else {
          btnText.set("Click!");
        }
      }
    });
  
    root.setCenter(button);

    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.setWidth(200);
    stage.setHeight(200);
    stage.show();
  }
}

The main focus would be on the EventHandler set for the ToggleButton, henceforth I would just show that part of the above code.

Lets see how we can use the Lambda expression to update above code.

 
button.setOnAction((ActionEvent event)-> {
  ToggleButton source = (ToggleButton) event.getSource();
  if (source.isSelected()) {
    btnText.set("Clicked!");
  } else {
    btnText.set("Click!");
  }
});

The only method in the EventHandler class is the handle(Event event) method and in the above example we just write the body and the parameter declaration for the method and remove all the unnecessary instantiation code.

We can remove the type declaration for the event, as the compiler will infer the type from the context. The context here is the Action event and hence the EventHandler expects a ActionEvent and thereby the event reference is inferred to be of ActionEvent type.

 
button.setOnAction((event)-> {
  ToggleButton source = (ToggleButton) event.getSource();
  if (source.isSelected()) {
    btnText.set("Clicked!");
  } else {
    btnText.set("Click!");
  }
});

This is just a peek at the Lambda expressions to be introduced in Project Lambda (JSR-335). I would take some time to dive a bit into detail of Lambda expressions.

Category: JavaTag: Java 8, Java FX, Project Lambda

About Mohamed Sanaulla

In his day job he works on developing enterprise applications using ADF. He is also the moderator of JavaRanch forums and an avid blogger.

Previous Post: « Developing a Simple Todo Application using JavaFX, Java and MongoDB- Part-3
Next Post: Virtual Extension Methods(or Defender Methods) in Java 8 »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact