15 October 2020

JavaFX Application Life Cycle


/**************************************************
*   Author: Morrison
*   Date:  15 Oct 2020
**************************************************/

import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
import javafx.scene.Scene;

public class App extends Application
{
    public App()
    {
    }
    
    @Override
    public void init()
    {
    }

    @Override
    public void start(Stage primary)
    {
        
        primary.show();
    }

    @Override
    public void stop()
    {
    }
}

Life Cycle

  1. The constructor executes and returns. Use this to initialize all state variables.
  2. init executes and returns. Use this to do anything needed for your app to run.
  3. start is called. This is the main loop of your application. It all runs in the JavaFX Application thread. All "live" activity must occur within this thread. All Scenes and Stages must be created here. This function returns under these cirucmstances.
    • The user hits the go-away button on the last stage and closes the app.
    • A widget, such as a button or menu item tells the app to quit
    • System.exit() or Platform.exit() is called.
  4. If System.exit() is called, the app quits dead in its tracks (bad). If Platform.exit() is called or the go-away button on the last window is hit, stop() is called. Since this method is abstract, it must be implemented by any JavaFX appliction.
  5. Execution terminates when stop() returns.
  6. The OS cleans up and reallocates the app's memory.

Essential Anatomy Lesson These are the basic parts of an Application.