12.3. High-Level Walkthrough

In JavaFX, applications (or apps) are created by:

  1. Creating a class that extends the javafx.application.Application class, and

  2. Overriding the inherited start method.

In the starter code, inspect the class declaration inside of ExampleApp.java. You should see the following class declaration:

public class ExampleApp extends Application {

Our class, ExampleApp is a child class of javafx.application.Application and therefore inherits instance variables and methods from that class. In order for our class to compile, we must override the start method since that method is declared abstract in the parent. We will explain the inside of the overridden start method in ExampleApp shortly.

The preferred way of launching (running) an instance of a JavaFX application class (e.g., ExampleApp), is by using the static Application.launch method inside of some separate driver class. The provided driver class is called ExampleDriver.java. Take a moment to inspect the main method inside of ExampleDriver.java:

The main method of the driver class (ExampleDriver.java)
public static void main(String[] args) {
   try {
      Application.launch(ExampleApp.class, args);
   } catch (Exception e) {
      System.err.println("The exception listed below occurred. If it pertains to DISPLAY,");
      System.err.println("then please logout, then log back in passing -XYC to ssh.");
      System.err.println(e.printStackTrace());
      System.exit(1);
   } // try
} // main

The try-catch statement is included to ensure that if any exceptions propogate up to main method, then the user is presented with a reason why the exception may have occurred along with the usual stack trace to help debugging. One such exception that happens fairly often relates to a timeout issue with the X-forwarding — in this case, we want to inform the user that they can log out, and then log back in to resolve the problem.

Note

If logging out and back in does not fix the issue, read the stack trace information to see why the error occurred.

The launch method launches the app by initiating the JavaFX application life-cycle, a sequence of steps that occur over the life of the launched application. The JavaFX runtime does the following, in order, whenever an application is launched:

JavaFX Application Life Cycle

  1. Constructs an instance of the specified Application subclass by calling its constructor;

  2. Calls the app’s init method, which the programmer may have overridden (we chose not to override this in ExampleApp.java;

  3. Creates a Stage object and calls the app’s start method with a reference to that Stage object;

  4. Waits for the app to finish, which happens when either of the following occur:

    • the app calls Platform.exit

    • the last window has been closed and the implicitExit attribute on Platform is true — this is the default.

  5. Calls the app’s stop method, which the programmer may have overridden (we chose not to override this in ExampleApp.java).