Introduction to JavaFX
JavaFX is a modern Java library for building graphical user interfaces (GUI) for desktop applications. It allows you to create windows, controls (buttons, text fields, etc.), graphics, animations, and multimedia elements. JavaFX is designed to run on devices that support Java, including Windows, macOS, and Linux.
JavaFX Architecture
JavaFX uses an internal layered architecture that separates responsibilities among different subsystems and allows the creation of high-performance, cross-platform graphical applications. This architecture describes how the JavaFX API communicates with the graphics engine, windowing system, multimedia, and operating system.
- JavaFX API: The top layer providing classes and packages for animations, UI controls, CSS styling, scene graph, events, multimedia, and application lifecycle.
- Scene Graph: Core of the graphical interface; a hierarchical tree of nodes (root, branch, leaf) representing visual components.
- Quantum Toolkit: Connects Prism (graphics engine) and Glass (window toolkit) with the API.
- Prism: Hardware-accelerated 2D/3D graphics engine; falls back to software rendering if GPU is unavailable.
- Glass Windowing Toolkit: Platform-dependent layer that manages windows, events, and surfaces.
- WebView: Embeds web content (HTML5, CSS, JavaScript) and allows interaction between Java and JavaScript.
- Media Engine: High-performance multimedia pipeline (GStreamer) for audio and video playback.
Logical Structure of a JavaFX Application
- Main Components
- Stage / Display – Main application window.
- Scene / Decor – The content (interface) visible in the window.
- Scene Graph – Hierarchical structure of elements (nodes) that form the interface. Each element (e.g., Button, Label, TextField) is a node.
- Node Each node represents a single element of the scene and has its own state, including:
- ID – unique identifier.
- Style – CSS styles applied to the node.
- Effects – visual effects (lighting, shadows, transformations).
- Event Handlers – event handling (clicks, mouse movement, key presses).
- State – current state (visibility, enabled/disabled, etc.).
JavaFX Application Lifecycle
To create a JavaFX application, your class must extend the abstract class javafx.application.Application. The lifecycle is managed through four main methods: init(), start(Stage stage), stop(), and the static method launch().
-
launch() – Start the application This static method is called first. It prepares the JavaFX environment (initializes threads, graphics pipeline, scene, etc.) and then creates an instance of the class extending
Application. Important:launch()is never called fromstart(); it is the entry point. -
init() – Initialization Called after the instance is created. At this point, JavaFX API is not ready, so UI elements or scenes should not be created. Suitable for:
- loading configuration files
- preparing data
- initializing business logic unrelated to the interface
Important: Called once per application lifecycle.
- start(Stage stage) – Launching the interface The main method where the UI is created.
Stageis the main window. Instart():
- Create a
Sceneand add layout containers, buttons, text fields, and other UI elements. - Set window title, size, and element visibility.
- Attach event handlers.
- Call
stage.show()to display the window.
Important: Called after init() on the JavaFX Application Thread; UI operations are thread-safe.
- stop() – Closing the application Called when the application is closing (e.g., user clicks “X”). Useful for:
- releasing resources (files, DB connections)
- stopping background threads
- saving state or settings
Ensures safe application termination without data loss.