Dr. Vatsal Shah
Subject Material

Java · JavaFX · Topic 3

JavaFX Application Lifecycle

Follow a JavaFX application from construction and launch through init(), start(), event processing, and stop().

Topic progress · 1 of 7 sections

Step 1 of 7

3.1 JavaFX Application Life Cycle

Learning objective

Understand the complete JavaFX application lifecycle before studying each callback separately.

Interactive concept lab

See the JavaFX lifecycle happen

Move one phase at a time and watch the framework, thread, source code, Stage, and console stay synchronized.

Current phase Ready to launch
LifecycleTraceDemo.java
01 public static void main(String[] args) {
02     Application.launch(args);
03 }
04 public LifecycleTraceDemo() {
05     System.out.println("Object created");
06 }
07 public void init() {
08     System.out.println("init()");
09 }
10 public void start(Stage stage) {
11     stage.setScene(createScene());
12     stage.show();
13 }
14 button.setOnAction(event -> {
15     label.setText("Event handled");
16 });
17 public void stop() {
18     System.out.println("Resources released");
19 }
Active thread Waiting to begin
JavaFX Runtime Click Next or Auto Play to launch the application.
StudentFX
Stage not created The interface appears during start(Stage).
Lifecycle consoleWaiting
Press Next to start the trace...

Teaching tip: pause at the Event Loop and simulate a button click to show why the application remains responsive after start() finishes.

Big picture

JavaFX creates the Application object and calls its lifecycle methods. The developer does not call init(), start(), or stop() directly.

After start(Stage) shows the window, the JavaFX Event Loop waits for button clicks, keyboard input, mouse actions, and other events. Closing the final window normally leads to stop().

Lifecycle stages and threads
StagePurposeThread
Application objectJavaFX constructs the Application subclassJavaFX launcher thread
init()Prepare configuration, data, and non-visual servicesJavaFX launcher thread
start(Stage)Create the scene graph and show the primary windowJavaFX Application Thread
Event LoopDispatch events and update the interfaceJavaFX Application Thread
stop()Release files, services, and other resourcesJavaFX Application Thread
LifecycleTraceDemo.java
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class LifecycleTraceDemo extends Application {
    public LifecycleTraceDemo() {
        System.out.println("Application object created");
    }

    @Override
    public void init() {
        System.out.println("init() - JavaFX thread: "
                + Platform.isFxApplicationThread());
    }

    @Override
    public void start(Stage stage) {
        System.out.println("start(Stage) - JavaFX thread: "
                + Platform.isFxApplicationThread());
        stage.setScene(new Scene(
                new StackPane(new Label("Close the window to call stop()")),
                390, 160));
        stage.setTitle("JavaFX Life Cycle");
        stage.show();
    }

    @Override
    public void stop() {
        System.out.println("stop() - JavaFX thread: "
                + Platform.isFxApplicationThread());
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Expected result
Application object created
init() - JavaFX thread: false
start(Stage) - JavaFX thread: true

After the window is closed:
stop() - JavaFX thread: true

Common mistakes

  • Calling lifecycle methods manually instead of using Application.launch().
  • Creating or changing live Scene Graph nodes inside init().
  • Running long tasks on the JavaFX Application Thread and freezing the interface.
  • Forgetting to release application-owned resources in stop().
Check your understanding: Which lifecycle methods run on the JavaFX Application Thread?

start(Stage) and stop() run on the JavaFX Application Thread; init() does not.

Step 2 of 7

3.2 Application Class

Learning objective

Explain why a JavaFX program extends Application.

The Application class controls the JavaFX lifecycle. A normal application creates a subclass and overrides start(Stage stage).

Application subclass
public class StudentApp extends Application {
    @Override
    public void start(Stage stage) {
        // Build and show the interface here.
    }
}
Check your understanding: Which class does a normal JavaFX application extend?

javafx.application.Application.

Step 3 of 7

3.3 main() Method

Learning objective

Explain the role and optional nature of main() in a JavaFX application.

Conventional main method
public static void main(String[] args) {
    Application.launch(args);
}

Some Java launch configurations can start an Application subclass without an explicit main() method. Including main() remains clear and portable for classroom programs.

Check your understanding: What should main() normally do in a JavaFX program?

Call Application.launch(args).

Step 4 of 7

3.4 launch() Method

Learning objective

Describe what happens when the JavaFX runtime is launched.

Application.launch() starts the JavaFX runtime, creates the Application instance, calls init(), and schedules start(Stage) on the JavaFX Application Thread.

Launch the runtime
Application.launch(args);
Check your understanding: Can Application.launch() be called repeatedly in the same JVM?

No. A JavaFX application launch is intended to occur once per JVM process.

Step 5 of 7

3.5 init() Method

Learning objective

Use init() for non-visual preparation and distinguish its thread from the UI thread.

Prepare non-visual state
private String message;

@Override
public void init() {
    message = "Application initialized";
}

Suitable work for init()

  • Read configuration
  • Prepare plain data
  • Create non-visual services
  • Validate startup arguments
Check your understanding: Should controls be created and added to the Scene in init()?

No. Scene Graph work belongs on the JavaFX Application Thread, normally inside start().

Step 6 of 7

3.6 start(Stage primaryStage)

Learning objective

Build the scene graph and display the primary window.

JavaFX calls start(Stage primaryStage) after init(). The supplied primary stage is the main window.

Build the interface in start()
@Override
public void start(Stage primaryStage) {
    Label label = new Label(message);
    Scene scene = new Scene(new VBox(10, label), 360, 160);
    primaryStage.setScene(scene);
    primaryStage.show();
}
Check your understanding: On which thread does JavaFX call start()?

The JavaFX Application Thread.

Step 7 of 7

3.7 stop() Method

Learning objective

Perform cleanup when a JavaFX application terminates and trace the complete lifecycle.

LifecycleDemo.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class LifecycleDemo extends Application {
    private String message;

    @Override
    public void init() {
        message = "Ready";
        System.out.println("init()");
    }

    @Override
    public void start(Stage stage) {
        System.out.println("start()");
        stage.setScene(new Scene(
                new VBox(10, new Label(message)), 300, 140));
        stage.setTitle("Lifecycle Demo");
        stage.show();
    }

    @Override
    public void stop() {
        System.out.println("stop()");
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}
Expected result
Console after launch:
init()
start()

Window: Lifecycle Demo displaying "Ready"

Console after the window closes:
stop()
Lifecycle order
PhaseResponsibility
ConstructionJavaFX creates the Application object
init()Prepare non-visual state
start(Stage)Build and show the interface
Event loopRespond to user actions
stop()Release resources before exit
Check your understanding: Which method should close application-owned files or network resources during shutdown?

stop().