JavaFX Basics and Hello World:

  • Isn’t part of the Java Standard Library
    • Must be included when running a JavaFX program
  • The window of the GUI is called the stage
  • The data inside of the window is called the scene
  • Must extend the application class
  • Must have the start method
    • 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:
    //Imports
    import 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 Application
    public 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
  • Some other events:
    • WindowEvent
    • InputEvent
      • KeyEvent
      • MouseEvent
      • TouchEvent

List of Node Types:

  • Layouts: FlowPlane, GridPlane, BorderPlane, HBox, StackPlane, VBox
  • Shapes: Circle, Rectangle
  • Images: Imageview
  • Controls: Button, CheckBox, TextField, Label
  • Root Node: The node at the top of the scene graph
  • Leaf Node: Any Nodes that are below the root node

Scene Graph and Stack Pane:

  • The scene graph is used to show the hierarchy of nodes inside of the stack pane Scene Graph

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 directly
EventHandler<ActionEvent> eventName = new EventHandler<>() {
  public void handle(ActionEvent event) {
    //Action
  }
}

Lambda Expressions:

  • Even more concise then Anonymous Inner Classes
//Same as code above
((ActionEvent event) -> {
  //Action
}
 
//Another example
event -> //Action