Java Reference
In-Depth Information
public interface Runnable{
public void run();
}
You can use this interface to create threads with different behaviors as follows:
Thread t = new Thread(new Runnable() {
public void run(){
System.out.println("Hello world");
}
});
With a lambda expression it would look like this:
Thread t = new Thread(() -> System.out.println("Hello world"));
2.4.3. GUI event handling
A typical pattern in GUI programming is to perform an action in response to a certain event
such as clicking or hovering over text. For example, if the user clicks the Send button, you may
wish to display a popup or perhaps log the action in a file. Again, you need a way to cope with
changes; you should be able to perform any response. In JavaFX you can use an EventHandler
to represent a response to an event by passing it to setOnAction:
Button button = new Button("Send");
button.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
label.setText("Sent!!");
}
});
Here, the behavior of the setOnAction method is parameterized with EventHandler objects.
With a lambda expression it would look like this:
button.setOnAction((ActionEvent event) -> label.setText("Sent!!"));
2.5. Summary
Following are the key concepts you should take away from this chapter:
 
Search WWH ::




Custom Search