Skip to main content

Posts

Showing posts from October, 2018

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