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

Sane JavaFX GUI Code

JavaFX GUI Sanity JavaFX GUI code can be quite chaotic. Such a situation makes it a challenge to understand the code and to troubleshoot it.  JavaFX GUIs are simple in concept but if you try hard enough, you can disorganise it. Disorganised code is a sign of confusion which is a sign of deeper malaise.... JavaFX graphic user interfaces can be built up very systematically by following a simple process: All the GUI is contained in a Stage . This Stage constitutes the visible application window though it does not have to be visible. On this stage is placed a Scene which contains Panes to organise and hold GUI controls such as buttons and text boxes. However, bad programming practice allows a programmer to add controls directly to the Scene . The problem with this practice is that there is no way to lay out such controls in a systematic pattern such as horizontal list or in a grid formation. Each Scene can have zero, one or more Panes . Panes can contain other Panes ...

JavaFX: After Netbeans what next?

I have taught JavaFX as a User Interface tool for a number of years. Just as it matured, the owners of Java decided to pull it out of the standard Java JDK, therefore,  using JavaFX is no longer a trivial matter. I used Netbeans to edit and run JavaFX code but Netbeans is also afloat, now owned by Apache Foundation. The new Apache Netbeans doesn't seem to run JavaFX without considerable effort. I have given up on Nerbeans. Whereto now? What do we use for our JavaFX? Do we give up on JavaFX and move on to some other UI tool? Python? plain old java with AWT/SWING? Not a chance. JavaFX has some elegance difficult to attain with these other tools. Its simple tool. Why complicate life by adopting ageing tools that just dont meet the demands of our fast-paced 21st century? Therefore, there was a need to figure out what can serve to build UIs. I choose JavaFX again but after some examination, I found that the IDE that seems to work and is actively being developed is Intellij IDEA. C...