public class FxmlView extends Object
Note that despite the conventional names, it is necessary to define fx:controller
attribute on the root node
of all FXML files (that have a corresponding controller).
The class supports also loading corresponding resource bundle file (if present) that is expected to follow the same naming convention e.g. Main_en_US.properties. The package structure would look in the following way:
com.mycompany.myapp.MainController.java com.mycompany.myapp.Main.fxml com.mycompany.myapp.Main.css com.mycompany.myapp.Main_en_US.properties
Example usage:
FxmlView mainView = new FxmlView(MainController.class); Scene scene = new Scene(mainView.getRootNode(), 400, 400); // ... MainController mainController = mainView.getController(); mainController.doSomething();
Controllers are instantiated using controller factory
that by default is
initialized to DefaultControllerFactory.createController(Class)
which supports basic Dependency Injection -
see doc of this class for details.
A custom factory can be specified to supply controllers e.g. from an external DI framework.
Alternatively to the controller factory
, the controller objects to be used by
a particular instance of FxmlView
can be also given directly to the
constructor
.
The class has been inspired by Adam Bien's FXMLView
from
afterburner.fx framework with the difference that it doesn't require a
separate view class per FXML.
Constructor and Description |
---|
FxmlView(Class<?> controllerClass,
Object... controllers)
Creates a new instance of the
FxmlView for the specified controllerClass and optional controller
instances. |
Modifier and Type | Method and Description |
---|---|
<C> C |
getController()
Returns the main controller of the view.
|
static Function<Class<?>,Object> |
getControllerFactory()
Returns the controller factory used to instantiate controllers.
|
<T extends Parent> |
getRootNode()
Returns the root node loaded from the FXML.
|
static void |
setControllerFactory(Function<Class<?>,Object> controllerFactory)
Sets the controller factory used to instantiate controllers.
|
public FxmlView(Class<?> controllerClass, Object... controllers)
FxmlView
for the specified controllerClass
and optional controller
instances. The controllerClass
is used to locate and load the corresponding FXML and optional CSS and
resource bundle property files.
The optional controllers
argument allows specifying directly the controller instances to be used instead
of, or in addition to the ones provided by the controller factory
i.e. the
controller factory is consulted only for classes whose instances are not provided in the constructor.
Example:
Main.fxml: ... <fx:include fx:id="header" source="Header.fxml"/> ... <fx:include fx:id="subView1" source="SubView.fxml"/> <fx:include fx:id="subView2" source="SubView.fxml"/> ... MainController mainController = ...; HeaderController headerController = ...; // The mainController and headerController instances will be used directly by the FxmlView, // however two distinct instances of SubViewController (corresponding to subView1 and subView2) will be created // using static controller factory FxmlView view = new FxmlView(MainController.class, mainController, headerController);
controllerClass
- class of the controller for which FXML should be loaded, together with an optional CSS and
resource bundle filecontrollers
- optional controller instances to be usedFXMLLoader.setController(Object)
,
FXMLLoader.setControllerFactory(javafx.util.Callback)
public <C> C getController()
C
- controller typeFXMLLoader.getController()
public <T extends Parent> T getRootNode()
T
- root node typeFXMLLoader.getRoot()
public static Function<Class<?>,Object> getControllerFactory()
public static void setControllerFactory(Function<Class<?>,Object> controllerFactory)
DefaultControllerFactory.createController(Class)
.controllerFactory
- the controller factoryNullPointerException
- if the factory is null
Copyright © 2019 CERN. All rights reserved.