12.3. High-Level Walkthrough¶
In JavaFX, applications (or apps) are created by:
Creating a class that extends the 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 how the constructor
and the overridden init, start, and stop methods in ExampleApp
work together 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 {
System.out.println("1. start the JavaFX runtime");
Application.launch(ExampleApp.class, args);
} catch (Exception cause) {
String message = "The exception listed below occurred. "
+ "If it pertains to DISPLAY, then please logout, "
+ "then log back in passing -XYC to ssh.";
System.err.printf("%s\n\n%s\n", message, cause);
cause.printStackTrace();
System.exit(1);
} // try
} // main
The driver prints 1. start the JavaFX runtime to standard output just
before calling Application.launch. The other numbered messages
come from the constructor and methods in ExampleApp.
The try-catch statement is included to ensure that if any
exceptions propagate 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(java.lang.Class, java.lang.String…) 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
Starts the JavaFX runtime, if it has not already started.
Constructs an instance of the specified
Applicationsubclass by calling its public no-argument constructor;Calls the app's init() method, which the programmer may have overridden (we override this in
ExampleApp.javato add the text to the root layout pane);Creates a
Stageobject and calls the app's start(javafx.stage.Stage) method with a reference to thatStageobject;Waits for the app to finish, which happens when either of the following occur:
the app calls 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 override this in
ExampleApp.javato print6. stop() called).
The runtime supplies the primary Stage object to our start method.
We do not need to create that object ourselves. Our constructor and the
init and start methods give us places to set up the app. Later,
our event handlers can respond to user input, and an overridden stop
method can perform cleanup. The inherited init and stop methods
are still called if we do not override them; their implementations do
nothing.
In ExampleApp, the constructor creates the HBox and Text
objects. The init method makes the text a child of the layout pane,
and start constructs the scene and sets up the window. The instance
variables let these methods work with the same objects.
The numbered messages help you follow this sequence. The constructor,
init, and start print messages 2, 3, and 4 when their calls begin.
At the end of start, the app prints 5. wait for the app to finish
after the call to show completes normally. This message marks the transition
to the waiting phase; the println call does not wait for the window
to close. The call to start completes normally after printing the
message, and the runtime processes events until the app finishes.
Closing the window normally causes the runtime to call stop, which
prints message 6.
Note
The JavaFX runtime calls the app's constructor and its start and
stop methods on a special thread called the JavaFX Application
Thread. Our event handlers also run on that thread. The init
method is different: the runtime calls it on a separate launcher
thread. For this reason, an app must not construct a Scene or a
Stage in its init method, but it may construct other JavaFX
objects, such as nodes, there. You can learn more about threads and
the JavaFX Application Thread in JavaFX Application Thread.