Java · JavaFX · Topic 3
JavaFX Application Lifecycle
Follow a JavaFX application from construction and launch through init(), start(), event processing, and stop().
Step 1 of 7
3.1 JavaFX Application Life Cycle
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.
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 }
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().
| Stage | Purpose | Thread |
|---|---|---|
| Application object | JavaFX constructs the Application subclass | JavaFX launcher thread |
| init() | Prepare configuration, data, and non-visual services | JavaFX launcher thread |
| start(Stage) | Create the scene graph and show the primary window | JavaFX Application Thread |
| Event Loop | Dispatch events and update the interface | JavaFX Application Thread |
| stop() | Release files, services, and other resources | JavaFX Application Thread |
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);
}
}Application object created
init() - JavaFX thread: false
start(Stage) - JavaFX thread: true
After the window is closed:
stop() - JavaFX thread: trueCommon 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
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).
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
Explain the role and optional nature of main() in a JavaFX application.
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
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.
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
Use init() for non-visual preparation and distinguish its thread from the UI thread.
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)
Build the scene graph and display the primary window.
JavaFX calls start(Stage primaryStage) after init(). The supplied primary stage is the main window.
@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
Perform cleanup when a JavaFX application terminates and trace the complete lifecycle.
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);
}
}Console after launch:
init()
start()
Window: Lifecycle Demo displaying "Ready"
Console after the window closes:
stop()| Phase | Responsibility |
|---|---|
| Construction | JavaFX creates the Application object |
| init() | Prepare non-visual state |
| start(Stage) | Build and show the interface |
| Event loop | Respond to user actions |
| stop() | Release resources before exit |
Check your understanding: Which method should close application-owned files or network resources during shutdown?
stop().