Handler Flashcards
Explain every line of code:
public class HandleEvent extends Application {
public void start(Stage primaryStage) {
OKHandlerClass handler1 = new OKHandlerClass();
btOK.setOnAction(handler1);
CancelHandlerClass handler2 = new CancelHandlerClass();
btCancel.setOnAction(handler2);
…
primaryStage.show(); // Display the stage
}
}
class OKHandlerClass implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent e) {
System.out.println("OK button clicked");
}
}</ActionEvent>
- The public voids start codeblock will display the the GUI to the user
- The 2 Handler functions are being used, one(OKHandlerClass) for if the user hits the OK button, the other(CancelHandlerClass) if they click on Cancel. Using btOK.setOnAction(handler1), the button will be active.
- When buttons are clicked the handle method in each handler class is activated.
What is the hierarchy of the Event classes?
-EventObject
-Event
-ActionEvent
-InputEvent
-MouseEvent
-KeyEvent
-WindowEvent
Note event classes are in the javafx.event package