JavaFX Tutorial – We shall learn to Create new Button and Set Action Listener in JavaFX Application.

Create new Button and Set Action Listener in JavaFX

Quick Code Snippet

Following is a step by step guide to create a new Button in JavaFX and Set Action Listener.

  1. Instantiate a new Button.

  2. Set some text in button.

  3. Set Action Listener

    If you have set an Action Listener and the button is clicked, EventHandler would execute the handle method. You should implement the handle method to perform an action after an event happens for button.

  4. Run the JavaFX Application.

    Following is the complete Class.

  5. Output

    Following would be the output on running the JavaFX Application.

    Create new Button and Set Action Listener in JavaFX - JavaFX Tutorial - www.tutorialkart.com

    Click on the Button

    If you click on the button ‘Display message’, ‘Hi  there! You clicked me!’ would be printed to console output. You may replace the code in handle() method to perform your own defined action.

 

Conclusion :

In this JavaFX Tutorial : Create new Button and Set Action Listener in JavaFX , we have learnt to create a new button with desired text and trigger an action when the button is clicked.