Skip to content

Deviating FXML location

Manuel Mauky edited this page Sep 25, 2017 · 1 revision

With mvvmFX, a typical "View" consists of a FXML file and a CodeBehind class. To load such a View you can either include the FXML file into another FXML file with the <fx:include src="..."> tag or directly load the View with the FluentViewLoader.

When using the FluentViewLoader to load a view, you provide the CodeBehind class reference to the view loader and the FXML file is resolved by following a set of [Naming Conventions], mainly that the CodeBehind class has to have the same name as the FXML file.

However, in certain situations it may be either not possible or unconvinient to follow these naming conventions. Starting with version 1.7 you can use the annotation @FxmlPath (TODO add link to javadoc) to bypass the naming conventions and define a deviating location for an FXML file.

With @FxmlPath you can define an absolute path to the FXML file (including file extension) for a specific CodeBehind. See the following file structure and the example code to load the View:

└── com
    └── example
        ├── myapp
        │   └── MyCodeBehind.java
        │   └── MyViewModel.java
        └── some
            └── other
                └── path
                    └── OtherName.fxml

MyCodeBehind:

@FxmlPath("/com/example/some/other/path/OtherName.fxml")
public class MyCodeBehind extends FxmlView<MyViewModel> {
    ...
}

With this annotation the View can be loaded with the FluentViewLoader as usual:

FluentViewLoader.fxmlView(MyCodeBehind.class).load();

Notice: The @FxmlPath annotation is only evaluated when loading a View with the FluentViewLoader. It is not used when a View is embedded in another FXML file with <fx:include src="" />. Instead the src attribute of the include-tag determines the path to the FXML file in this case.

When should @FxmlPath be used?

We encourage you to stick with the naming conventions and to not use @FxmlPath if possible. @FxmlPath is intended as a tool to enable certain advanced setups. There are at least two situations where @FxmlPath can be useful:

  1. If you can't control the file structure or naming of the FXML or CodeBehind.
  2. With the annotation it is now possible to have multiple CodeBehind classes for a single FXML file.