View RSS Feed

A JavaFX Blog

The Life-Cycle Methods of a JavaFX Application

Rating: 2 votes, 5.00 average.
by , 12-09-2016 at 07:51 PM (4880 Views)
The methods of the javafx.application.Application class that define the life cycle of a JavaFX application are the following:

  • public void init()
  • public abstract void start(Stage primaryStage)
  • public void stop()


The init() method is called right after an Application object is instantiated, and can be used for application-specific initializations; however, GUI operations such as creating stages or scenes cannot be done from within this method, because at the time this method is called the JavaFX framework is not ready yet for creating a graphical user interface.

The start() method is called after the JavaFX framework has created the thread that will manage the graphical user interface of the application, and is called from within that thread. A primary stage for the application (corresponding to its main window) is supplied as argument, and the class implementation can create a scene, populate it with graphic components, assign the scene to the primary stage, and show the stage.

When an application is about to terminate (for example, because the user has clicked the close button on the application’s window), The JavaFX framework calls the stop() method on the Application object. Here, any cleanup tasks appropriate for the application can be done.

In simple applications, there is usually no need to override the init() and stop() methods (the Application base class contains default empty implementations of these methods), but the start() method must always be implemented by any non-abstract Application subclass.

The following minimalistic Application subclass prints on the standard output a message when the application is launched and when each of the three above methods is called.

Java Code:
public class MinimalisticApplication extends Application {
  public static void main(String[] args) {
    System.out.println("Launching JavaFX application");
    launch(args);
  }
  public void init() {
    System.out.println("init() method");
  }
  public void start(Stage primaryStage) {
    System.out.println("start() method");
    primaryStage.setTitle("Hello World!");
    primaryStage.show();
  }
  public void stop() {
    System.out.println("stop() method");
  }
}
The messages printed on the standard output as soon as the application is launched are:

Launching JavaFX application
init() method
start() method

While when the application window is closed, the following message is printed:

stop() method

Submit "The Life-Cycle Methods of a JavaFX Application" to Facebook Submit "The Life-Cycle Methods of a JavaFX Application" to Digg Submit "The Life-Cycle Methods of a JavaFX Application" to del.icio.us Submit "The Life-Cycle Methods of a JavaFX Application" to StumbleUpon Submit "The Life-Cycle Methods of a JavaFX Application" to Google

Updated 12-11-2016 at 07:16 PM by JavaFX

Tags: None Add / Edit Tags
Categories
JavaFX Application