Exploring JavaFX's Application class

Tour JavaFX's Application class and learn about its capabilities

1 2 Page 2
Page 2 of 2

Notifying the preloader with an application-generated notification

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:

  • receives progress notification about the loading of application resources
  • receives notification of errors
  • receives notification of application initialization and startup
  • decides when the application should become visible

Application declares the following method for interacting with a preloader:

  • void notifyPreloader(Preloader.PreloaderNotification info): Notify the preloader with an application-generated notification. Application code calls this method with the specified notification, which is an object created from the javafx.application package's Preloader.ErrorNotification, Preloader.ProgressNotification, or Preloader.StateChangeNotification class. The notification is delivered to the preloader's handleApplicationNotification() method. notifyPreloader() is primarily useful for cases where an application wants the preloader to show progress during a long application initialization step.

Setting and getting the application's user agent style sheet

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:

  • STYLESHEET_CASPIAN: The default style sheet in JavaFX 2.x.
  • STYLESHEET_MODENA: The default style sheet in JavaFX 8.x.

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:

  • void setUserAgentStylesheet(String url): Set the user agent style sheet for the entire application. This style sheet provides default styling for all user interface controls and other nodes. Each JavaFX release may have a new default style sheet so you would call this method to guarantee consistency. The style sheet is identified by a string-based URL. Presently, you would pass one of the aforementioned Application constants to url. Pass null to restore the platform default style sheet. This method must be called on the JavaFX Application Thread.

    You can also set this property on the command line via option -Djavafx.userAgentStylesheetUrl=[URL]. Setting the property via the command line overrides setting the property via this method.

  • String getUserAgentStylesheet(): Return the user agent style sheet (e.g., CASPIAN) that's used by the entire application. A null return value means the platform default style sheet is being used. This method must be called on the JavaFX Application Thread.

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

Listing 5. GlobalSS.java

import javafx.application.Application;
import javafx.application.Platform;

import javafx.stage.Stage;

public class GlobalSS extends Application
   public void start(Stage primaryStage)
      System.out.printf("Default global style sheet: %s%n",
      System.out.printf("New global style sheet: %s%n",
      System.out.printf("New global style sheet: %s%n",

Compile Listing 5 as follows:

javac GlobalSS.java

Run the resulting GlobalSS.class as follows:

java GlobalSS

You should observe the following output:

Default global style sheet: null
New global style sheet: CASPIAN
New global style sheet: MODENA


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.

Get the source code for this post's applications. Created by Jeff Friesen for JavaWorld

The following software was used to develop the post's code:

  • 64-bit JDK 8u60

The post's code was tested on the following platform(s):

  • JVM on 64-bit Windows 8.1

This story, "Exploring JavaFX's Application class" was originally published by JavaWorld.

1 2 Page 2
Page 2 of 2
ITWorld DealPost: The best in tech deals and discounts.
Shop Tech Products at Amazon