FXML is an XML-based markup language that JavaFX uses to describe a user interface declaratively. It moves the scene graph (layouts, controls, styles, event hooks) out of Java code and into a separate .fxml file, which FXMLLoader turns into real JavaFX objects at runtime. This keeps view and logic apart, lets designers work in Scene Builder, and makes screens easier to read, change and test.
JavaFX has been a separate OpenJFX download since Java 11, so FXML support comes from the javafx-fxml module, not from the JDK itself.
An FXML document is a tree of elements. Each element names a JavaFX class, and attributes set its properties. Classes are brought in with <?import ...?> processing instructions, just like import in Java.
The fx:controller attribute names the class that handles events, fx:id gives an element a name the controller can inject, and #onHelloClicked binds the button's action to a controller method. Elements nested inside a layout such as VBox become its children automatically.
The controller is a plain Java class. Fields annotated with @FXML are filled in by the loader, and annotated methods can be referenced from the markup. A no-argument initialize() method, if present, is called after all fields are injected.
The application class loads the file and puts the result in a scene:
If the resource path is wrong, getResource returns null and loading fails with a confusing exception, so keep FXML files on the classpath next to your resources and double-check the path. On a modular project you must also opens com.example to javafx.fxml; in module-info.java, otherwise the loader cannot reach into your controller.
| Benefit | What it means in practice |
|---|---|
| Declarative structure | The hierarchy of panes and controls is readable at a glance, and it matches what is on screen. |
| Separation of concerns | Layout lives in XML, behaviour in the controller. Changing a margin does not touch Java code. |
| Designer tooling | Scene Builder opens and edits the same .fxml files with drag and drop, so designers can contribute. |
| Shorter Java classes | No long chains of new, getChildren().add(...) and setter calls. |
| CSS styling | Attach a stylesheet with stylesheets and use styleClass or id selectors instead of inline styling in code. |
| Localisation | Pass a ResourceBundle to the loader and write %key in attributes to pull text from properties files. |
Scene Builder is no longer shipped by Oracle. It is maintained by Gluon and downloaded separately, and most IDEs can be pointed at it to open FXML files.
Attributes can reference values from the controller, a resource bundle or a CSS file. Text prefixed with % is looked up in the bundle, and @ refers to a resource relative to the FXML file:
Calling loader.getController() after load() is the standard way to pass data into a screen, for example to hand it a model or a service. You can also supply a controller factory with loader.setControllerFactory(...), which is how dependency-injection containers create controllers with constructor injection.
FXML is not mandatory. Building the scene graph in Java is fine for small tools, for highly dynamic screens and for custom controls whose structure depends on data. The trade-offs:
| Aspect | FXML | Java code |
|---|---|---|
| Readability of layout | High, mirrors the tree | Lower for deep trees |
| Compile-time safety | None for ids and handler names, errors appear at load time | Full type checking |
| Refactoring | Renaming a field or method needs a manual update in the markup | IDE refactoring covers everything |
| Visual editing | Yes, with Scene Builder | No |
| Dynamic content | Possible, but awkward | Natural |
A common compromise is to define static screens in FXML and generate repeated or data-driven parts, such as list rows, in Java or with a cell factory. Custom components can also be written with fx:root, which lets a class act as the root element of its own FXML file.
No. Everything FXML does can be done in Java code. FXML is a convenience that separates layout from logic and enables visual editing in Scene Builder.
@FXML field null?The usual causes are an fx:id that does not match the field name, a missing fx:controller, using the field in the constructor (injection happens after construction, so use initialize()), or a controller created manually with new instead of by the loader.
Yes. The FXML file is the view, the controller class mediates, and your own domain classes are the model. Pass the model into the controller after loading or through a controller factory.
Use <fx:include source="header.fxml"/> to embed another file, or create a custom component with fx:root and type. Each included file can have its own controller.
FXML describes a JavaFX interface as XML, with FXMLLoader building the objects and a controller class reacting to events through @FXML fields and methods. It keeps layout out of Java code, works with Scene Builder, supports CSS and resource bundles, and fits MVC well. Its main costs are errors that surface only at load time and extra care with module opens declarations, so use it for structured screens and keep Java code for dynamic parts.