FXML is an XML-based markup language used in JavaFX to describe a user interface declaratively. Its purpose is to separate the layout of the UI from the application logic, so designers can edit .fxml files, tools such as Scene Builder can generate them, and Java controller classes hold the behaviour.
An FXMLLoader reads the file at runtime, creates the node objects, wires them to a controller and returns the root of the scene graph.
Building a JavaFX scene in Java code works, but a nested layout quickly becomes hard to read. FXML expresses the same scene graph as a tree of elements, where each element is a JavaFX class and each attribute is a property. The benefits are:
stylesheets and styleClass, so look and feel stays in CSS.%key syntax.FXML is not required. Anything you can write in FXML you can build in Java, and many small applications do exactly that.
Save this as login.fxml in the same package path as your controller, under src/main/resources.
The fx:controller attribute names the controller class, fx:id makes a node available to the controller, and #handleGreet binds the event to a controller method.
The @FXML annotation lets the loader inject fields and call handlers even when they are private. After injection the loader calls initialize() if the controller has one. If the loader returns null for getResource, the path is wrong; this is the most common first-run error.
Scene Builder is a separate visual tool (now maintained by Gluon) that opens and writes .fxml files, so you can drag controls onto a canvas instead of typing XML. Styling uses a JavaFX CSS file attached with stylesheets="@app.css" on the root element.
Since JavaFX is no longer bundled with the JDK, you add it as a dependency, and if your project uses the Java module system you must open the controller package to the FXML module in module-info.java:
Without the opens line the loader cannot reflectively reach @FXML members and fails with an access error.
| Aspect | FXML | Java code |
|---|---|---|
| Readability of layout | High, mirrors the node tree | Gets verbose when nested |
| Visual editing | Scene Builder | None |
| Compile-time checks | None, errors appear at load time | Full type checking |
| Refactoring | Strings for ids, handlers and classes | IDE refactoring works directly |
| Dynamic UI | Possible but awkward | Natural |
A common compromise is FXML for static structure and Java for dynamic parts.
No. It is optional. You can build the whole scene graph in Java, which is often simpler for small tools.
It gives an element a name. If the controller has a field with the same name annotated with @FXML, the loader injects the node into it.
Load the file with an FXMLLoader instance, call loader.load(), then call loader.getController() and use its methods or setters. You can also supply a controller factory for dependency injection.
Typical causes are a wrong resource path, a missing <?import?> for a class, a controller without a public or @FXML no-argument constructor, or a handler name that does not exist in the controller.
FXML defines a JavaFX user interface in XML so that layout stays apart from logic. You connect the two with fx:controller, fx:id and @FXML, load the file with FXMLLoader, and can design it visually in Scene Builder. Use it for structured screens and keep code-only UI for highly dynamic parts.