1  import javafx.application.Application;
  2  import javafx.geometry.Pos;
  3  import javafx.scene.Scene;
  4  import javafx.scene.control.Button;
  5  import javafx.scene.layout.HBox;
  6  import javafx.stage.Stage;
  7  import javafx.event.ActionEvent;
  8  import javafx.event.EventHandler;
  9  
 10  public class HandleEvent extends Application {
 11    @Override // Override the start method in the Application class
 12    public void start(Stage primaryStage) {
 13      // Create a pane and set its properties
 14      HBox pane = new HBox(10);
 15      pane.setAlignment(Pos.CENTER);
 16      Button btOK = new Button("OK");
 17      Button btCancel = new Button("Cancel");
 18      OKHandlerClass handler1 = new OKHandlerClass();
 19      btOK.setOnAction(handler1);
 20      CancelHandlerClass handler2 = new CancelHandlerClass();
 21      btCancel.setOnAction(handler2);
 22      pane.getChildren().addAll(btOK, btCancel);
 23      
 24      // Create a scene and place it in the stage
 25      Scene scene = new Scene(pane);
 26      primaryStage.setTitle("HandleEvent"); // Set the stage title
 27      primaryStage.setScene(scene); // Place the scene in the stage
 28      primaryStage.show(); // Display the stage
 29    }
 30  
 31    /**
 32     * The main method is only needed for the IDE with limited
 33     * JavaFX support. Not needed for running from the command line.
 34     */
 35    public static void main(String[] args) {
 36      launch(args);
 37    }
 38  } 
 39  
 40  class OKHandlerClass implements EventHandler<ActionEvent> {
 41    @Override
 42    public void handle(ActionEvent e) {
 43      System.out.println("OK button clicked"); 
 44    }
 45  }
 46  
 47  class CancelHandlerClass implements EventHandler<ActionEvent> {
 48    @Override
 49    public void handle(ActionEvent e) {
 50      System.out.println("Cancel button clicked");
 51    }
 52  }