JavaFX Controller Support
This VSCode extension provides support for writing FXML controllers in JavaFX projects. It is a helper for people who are learning JavaFX. Once JavaFX is no longer new to you, feel free to remove it.
- Finds and fixes
fx:id errors.
- Warns when an FXML file has no controller class, or when its controller class is not in the project.
- Offers a Code Lens option to add an
initialize method when the controller class has none.
Features
(1) Finding and Fixing fx:id Errors
Shows a warning when an fx:id in the FXML has no field annotated with @FXML in the controller class.
🛠️ Adds the @FXML Fields You Need
Quick Fix adds a missing field, one fx:id at a time. The field is private and annotated with @FXML.
🔍 Code Lens Option for Missing @FXML Fields
Provides a Code Lens option, "Add all missing @FXML fields," which adds every missing field at once. The fields are private and annotated with @FXML.
📺 Warnings for Incorrect @FXML Fields
Shows a warning when a field annotated with @FXML has no fx:id in the FXML.
The extension ignores fields named location and resources. FXMLLoader fills them by the name of the field, not from an fx:id. See Initializable.
(2) Warnings for a Missing Controller
💡 No controller for this FXML. Specify fx:controller, or call setController() when loading it.
💡 Missing …/src/main/java/com/example/MyAppController.java
The first warning appears when no controller class is paired with the FXML file. The second appears when a controller class is paired but its .java file is not in the project.
The extension reads setController() calls from the compiled class files, so build the project first. If a workspace folder holds no class file, the extension shows no warning for the FXML files in it. It cannot read every setController() call. See Limitations.
(3) Code Lens Option to Add initialize Method
Press "Add @FXML private void initialize() method" to add the initialize method.
Result:
Miscellaneous
Limitations
Event handlers
This extension does not support references to event handlers using @FXML, and there are no plans to support them.
Give the element an fx:id, then set the event handler in the controller class.
Pairing a controller class with an FXML file
A controller class is paired with an FXML file in one of two ways:
- the
fx:controller attribute in the FXML file
- a call to
FXMLLoader.setController() in Java
The second way has three rules.
Write both calls in the same class. The new FXMLLoader(...) that takes the FXML file name and the setController() call must be in the same class.
The extension can read this pair:
FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
loader.setController(new MainController());
Write the FXML file name as a string literal. A name that the program builds while it runs cannot be read.
The extension cannot read this name:
FXMLLoader loader = new FXMLLoader(getClass().getResource(name + ".fxml"));
Create the controller object in the same class. A controller whose class is decided while the program runs cannot be read.
The extension cannot read this controller:
void wire(Object controller) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
loader.setController(controller);
}
The extension then finds no pair. It shows no warning for that FXML file.
Public fields
FXMLLoader also fills a public field that has no annotation. This extension recognises a field only by its @FXML annotation.
Included FXML files
This extension does not support FXML files that are loaded using fx:include.
Requirements
- Java and FXML files must be inside the
src directory. For example, put Java files in src/main/java/com/example/FooController.java and FXML files in src/main/resources/com/example/foo.fxml.
- A workspace folder may hold more than one project. Each module of a multi-module build uses its own
src directory.
- To pair an FXML file with a controller class, name the class in
fx:controller, or pass it to FXMLLoader.setController(). See Limitations above.
Issues
https://github.com/sosuisen/javafx-controller-support/issues
Release Notes
1.3.0
- You can now pair an FXML file with its controller class by
FXMLLoader.setController(). The extension reads the call from the compiled class files, so build the project first.
- The warning for an FXML file with no controller class now reads
No controller for this FXML. Specify fx:controller, or call setController() when loading it. The warning does not appear while the workspace folder holds no class file.
- A workspace folder may hold more than one project. The extension now looks for the controller class in the project that holds the FXML file.
- The extension now reads a folder that you add to the workspace while VS Code is running.
1.2.0
1.1.0
- Added Builder Class Generator.
1.0.0