Exploring JavaFX's Application class

14.04.2016
JavaFX applications are based on JavaFX's Application class. Perhaps you are unfamiliar with this class and have questions about using Application and on what this class offers your application code. This post attempts to answer these questions while exploring Application.

The javafx.application.Application class provides a framework for managing a JavaFX application. This application must include a class that extends Application, overriding various methods that the JavaFX runtime calls to execute application-specific code.

An application can call Application methods to obtain startup parameters, access host services, arrange to launch itself as a standalone application, interact with the preloader (a small application that's started before the main application to customize the startup experience), and access the user agent (Web browser) style sheet.

One of Application's tasks is to manage the application's life cycle. The following overridable Application methods play a role in this life cycle:

The JavaFX runtime interacts with an application and invokes these methods in the following order:

JavaFX creates an application thread, which is known as the JavaFX Application Thread, for running the application's start() and stop() methods, for processing input events, and for running animation timelines. Creating JavaFX Scene and Stage objects as well as applying scene graph modification operations to live objects (those objects already attached to a scene) must be done on the JavaFX Application Thread.

The java launcher tool loads and initializes the specified Application subclass on the JavaFX Application Thread. If there is no main() method in the Application class, or if the main() method calls Application.launch(), an instance of the Application subclass is constructed on the JavaFX Application Thread.

The init() method is called on the JavaFX Launcher Thread, which is the thread that launches the application; it's not called on the JavaFX Application Thread. As a result, an application must not construct a Scene or Stage object in init(). However, an application may construct other JavaFX objects in the init() method.

Listing 1 presents a simple JavaFX application that demonstrates this life cycle.

Compile Listing 1 as follows:

Run the resulting LifeCycle.class as follows:

You should observe the following output:

The output reveals that init() is called on a different thread than start() and stop, which are called on the same thread. Because different threads are involved, you may need to use synchronization.

If you comment out Platform.exit(), you won't observe the stop() called on thread Thread[JavaFX Application Thread,5,main] message because the JavaFX runtime won't invoke stop() -- the application won't end.

Application provides the Application.Parameters getParameters() method for returning the application's parameters, which include arguments passed on the command line, unnamed parameters specified in a JNLP (Java Network Launch Protocol) file, and <name,value> pairs specified in a JNLP file.

Application.Parameters encapsulates the parameters and provides the following methods for accessing them:

Listing 2 presents a simple JavaFX application that demonstrates these methods.

Compile Listing 2 as follows:

Run the resulting Parameters.class as follows:

You should observe the following output:

Application provides the HostServices getHostServices() method for accessing the host services provider, which lets the application obtain its code and document bases, show a Web page in a browser, and communicate with the enclosing Web page using JavaScript when running in a browser.

The javafx.application.HostServices class declares the following methods:

Listing 3 presents a simple JavaFX application that demonstrates most of these methods.

Compile Listing 3 as follows:

Run the resulting HostServ.class as follows:

You should observe something similar to the following output:

A JavaFX application doesn't require a main() method. The JavaFX runtime takes care of launching the application and saving command-line arguments. However, if you need to perform various tasks before the application is launched, you can specify a main() method and have it invoke one of the following static methods:

Listing 4 presents a simple JavaFX application that demonstrates the second launch() method.

Compile Listing 4 as follows:

Run the resulting Launch.class as follows:

You should observe the following output:

An application may contain a preloader that's used to improve the application-loading experience, especially for applications that are embedded in a browser or launched in WebStart execution mode. The preloader accomplishes the following tasks, notifying the user about what's happening while the application is loading:

Application declares the following method for interacting with a preloader:

JavaFX lets you determine the overall style of your application's user interface (e.g., controls) by installing a global style sheet. Two style sheets are available, which are represented by the following String constants that are defined in the Application class:

If a style sheet is not explicitly installed, a default style sheet (either Caspian or Modena, which is determined by the JavaFX version) is used. Application provides the following static methods for setting a new style sheet and getting the current style sheet:

Listing 5 presents a simple JavaFX application that demonstrates these methods.

Compile Listing 5 as follows:

Run the resulting GlobalSS.class as follows:

You should observe the following output:

Application is essential to the architecture of a JavaFX application. In a future post, I'll demonstrate the applet and Java WebStart application styles of JavaFX applications. Also, I'll dig into Stage and related classes to address questions about their role in bringing a JavaFX application to visual life.

(www.javaworld.com)

Jeff Friesen

Zur Startseite