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
12 public void start(Stage primaryStage) {
13
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
25 Scene scene = new Scene(pane);
26 primaryStage.setTitle("HandleEvent");
27 primaryStage.setScene(scene);
28 primaryStage.show();
29 }
30
31
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 }