We have implemented EventHandler interface in the Main.java this time. So this class can simply handle all the events for the scene.

We need to override the hande() method of the EventHandler interface in Main class. This method handles all type of events for the application. So we need to check which on which layout has the current event occured. So we added a check for the button in the handle() method for all the events called on buttons.

Now run the Main.Java file as Java Application and click on the button. You will see ‘Hey Charlie!’ printed on the console.

Thus we have handled click events for the button.

Anonymous Inner Classes And Lambda Expressions

The above example we saw for event handling had too much work to be done for just a single event. Lets look at a different implementation for the same task as before using Lambda Expressions

Look at the commented section of the code above. The commented section is another way of doing the same EventHandling using anonymous class. You might be already familiar with that method.
The uncommented setOnAction() method which has been called uses Lambda Expressions for quickly handling the event. This feature has been added in JDK8 only. So you might face syntax errors if you are using lower versions of JDK.

Switching Scenes

Now lets learn about changing the scene i.e whole layout of the stage of our application. Its similar to switching fragments in an Android Application.