Java · JavaFX · Topic 2
JavaFX Application Lifecycle
Follow a JavaFX application from construction and launch through init(), start(), event processing, and stop().
Step 1 of 6
2.1 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 2 of 6
2.2 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 3 of 6
2.3 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 4 of 6
2.4 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 5 of 6
2.5 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 6 of 6
2.6 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().