12.3. High-Level Walkthrough¶
In JavaFX, applications (or apps) are created by:
Creating a class that extends the javafx.application.Application class, and
Overriding the inherited
startmethod.
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:
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.");
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
Constructs an instance of the specified
Applicationsubclass by calling its constructor;Calls the app’s init method, which the programmer may have overridden (we chose not to override this in
ExampleApp.java;Creates a
Stageobject and calls the app’s start method with a reference to thatStageobject;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
implicitExitattribute onPlatformis true — this is the default.Calls the app’s stop method, which the programmer may have overridden (we chose not to override this in
ExampleApp.java).