Preforms the setup needed to create the JavaFX application
The start method is then called by launch, which organizes the stage and scene
Must be created and passes in a Stage parameter
A main method is not needed
Java automatically creates a state to pass into start
Hello World:
//Importsimport javafx.application.Application;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.StackPane;import javafx.stage.Stage;//Extends Applicationpublic class HelloWorld extends Application { public static void main(String[] args) { launch(args); //Runs start method } public void start(Stage mainStage) { mainStage.setTitle("Hello World Program"); //Title of window Button btn = new Button(); //Creating button btn.setText("Print Hello World!"); //Setting text of button btn.setOnAction(new CustomEventHandler()); //Runs CustomEventHandler() method StackPane root = new StackPane(); //Creates new StackPane root.getChildren().add(btn); //Add button to the StackPane Scene scene = new Scene(root, 300, 300); //Creating new Scene mainStage.setScene(scene); //Sets the created scene to the stage mainStage.show(); //Displays the stage } private class CustomEventHandler implements EventHandler<ActionEvent> { public void handle(ActionEvent event) { System.out.println("Hello World!"); //Prints to terminal } }}
The Stage and Scenes:
Stage
Only one
The window of the app
Contains all of the displayed data
Scenes
Multiple
The content being displayed on stage
Event Handling:
Clicking a button creates an ActionEvent object that contains the actions of the user
ActionEvent is a child class of Event
ActionEvent fires an EventHandler object that has a handle method which runs the expected behavior
Every EventHandler class must implement EventHandler
Functional Interface: An interface with only one abstract method
The scene graph is used to show the hierarchy of nodes inside of the stack pane
Anonymous Inner Classes:
Classes that are used for one specific case
More concise then writing an entire class
Don’t have a name
// Can remove eventname if used directlyEventHandler<ActionEvent> eventName = new EventHandler<>() { public void handle(ActionEvent event) { //Action }}