Skip to main content

LAMBDA EXPRESSIONS FOR HANDLING EVENTS

Event handlers form the core of any application. They define what happens when you click a button, press a key, select a menu and so on.
The discussion on this site demonstrates how to use Lambda expressions to handle events. It is elegant and greatly simplifies your code.
The old way of handling a button-click looks like this:
Button btn = new Button();
btn.setText("Say 'Hello World'");

 btn.setOnAction(new EventHandler<ActionEvent>()
{

      @Override
      public void handle(ActionEvent event)
      {
          System.out.println("Hello World!");
      }
 });
The following is a Lambda expression using a "goes to" operator (->):
Button button = new Button("Click");

        button.setOnAction(value ->  {
           label.setText("Clicked!");
        });
Even more elegant:
someButton.setOnAction(evt -> someMethod())

Now a greater improvement in programming style: This last format allows you to neatly line up your event handler methods as follows:
public class Test1 extends Application
{

@Override
public void start(Stage primaryStage)
{
    Button btn = new Button();
    btn.setText("Say 'Hello World'");

    btn.setOnAction(event -> handler());

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

    Scene scene = new Scene(root, 300, 250);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
   primaryStage.show();
}

  public void handler()
  {
      System.out.println("Hello World!");
  }
}

Now you have no excuse to write crappy code. Be elegant and lucid and fast.

Comments

Post a Comment

Popular posts from this blog

How to Run JavaFX 11+ in Intellij IDEA

First published: 8th June 2019 This page explains: 1. How to set up Intellij IDEA with JavaFX 11+ 2. How to configure Intellij IDEA to run JavaFX programs. Follow the links below to download the guides: 1. JavaFX 1 - Installing IntelliJ IDEA and JavaFX 11 Tutorial (8-6-19) 2.  Configure Intellij IDEA to run JavaFX programs

Android Studio: The Continuing Nightmare [caution, annoyed brother ahead]

If you install Android Studio and it works, consider that a major miracle. Otherwise expect messages like: Gradle doesn't work. Fix you project! [With a saw and hammer, right?] Access denied! Access denied! None of your AVD work so no emulation... FileNotFoundException! - What file please? And does AS expect me to help find it? Why can't YOU find it?  I wish it was kickable - I would give it a very giant KICK. Update! [1.5GB] At lunch - Update! 1GB Evening - Update! Is update some kind of chronic disease or what? And who's going to pay for all these bundles. (9 GB today alone. It's an embarrassment for Google. This thing is like the old steam locomotive: it needs water and wood every few miles. Indexing...………………………………………………………………… an empty project...……………………………………………………. for 30min...……………………………! Get serious Google. If its dead, bury it. Start all over from scratch. AVD won't load Can't find AVD...……….. Java 10.1 is an invalid JDK…………… Can't