Skip to content

mvvmFX overview core classes

Manuel Mauky edited this page Sep 8, 2015 · 3 revisions

Package: de.saxsys.mvvmfx

This package contains the interfaces and classes that are needed for the basic Model-View-ViewModel pattern.

FxmlView

This interface has to be implemented as a marker for Views that are using FXML to describe ui. According to the Naming Conventions there has to be a FXML file with the same name as the class in the same package.

JavaView

This interface has to be implemented as a marker for Views that are directly implementing the UI in java Code. It's common that a View class implementing this interface does also extend from a JavaFX component class like VBox or Control.

ViewModel

This interface has to be implemented by the ViewModel. There is no special naming convention for the ViewModel but it's recommended to match the name of the View. For example when your View is named "PersonView" a good name for the ViewModel would be "PersonViewModel".

@InjectViewModel

This annotation is used to tell the mvvmFX framework to inject the corresponding ViewModel into the View.

The typical usage can be seen in this example:

public class PersonView implements FxmlView<PersonViewModel> {
    @InjectViewModel
    private PersonViewModel viewModel;
    ...
}

It is garanteed that the viewModel will be injected before the initialize method of the View is called. This is true for both the explicit initialize method that has to be implemented when you use JavaFX's javafx.fxml.Initializeable interface and the implicit initialize method that is based on naming conventions of the FXMLLoader (see here).

Notice for CDI/Guice: Even when using a Dependency Injection like CDI or Guice which uses @Inject for injection of references, you still have to use @InjectViewModel to inject the ViewModel for the given View. There are some additional checks and logic done in the background when we inject the ViewModel via the @InjectViewModel annotation that wouldn't be done when using @Inject. However, the ViewModel instance that we inject is still managed by the Dependency Injection framework! Internally we are taking the ViewModel instance from the DI framework before injecting it into the View.

FluentViewLoader

This class provides a fluent API to load Views. For more informations see FluentViewLoader.

ViewTuple

The class ViewTuple is the result that you get when you load a view with the FluentViewLoader. This class contains references to the loaded codeBehind class, the ViewModel and the root node that can be added to the Scene-Graph of your application.