mvvmfx/src/main/java/de/saxsys/mvvmfx/internal/viewloader/ViewLoaderReflectionUtils.java
public static void injectViewModel(final View view, ViewModel viewModel) {
        if (viewModel == null) {
            return;
        }
        final Optional<Field> fieldOptional = getViewModelField(view.getClass(), viewModel.getClass());
        if (fieldOptional.isPresent()) {
            Field field = fieldOptional.get();
            ReflectionUtils.accessMember(field, () -> {
                Object existingViewModel = field.get(view);
                if (existingViewModel == null) {
                    field.set(view, viewModel);
                }
            }, "Can't inject ViewModel of type <" + viewModel.getClass() + "> into the view <" + view + ">");
        }
    }
public static void injectViewModel(final View view, ViewModel viewModel) {
        if (viewModel == null) {
            return;
        }
        final Optional<Field> fieldOptional = getViewModelField(view.getClass(), viewModel.getClass());
        if (fieldOptional.isPresent()) {
            Field field = fieldOptional.get();
            ReflectionUtils.accessMember(field, () -> {
                Object existingViewModel = field.get(view);
                if (existingViewModel == null) {
                    field.set(view, viewModel);
                }
            }, "Can't inject ViewModel of type <" + viewModel.getClass() + "> into the view <" + view + ">");
        }
    }
public static void injectViewModel ( final View view , ViewModel viewModel ) { if ( viewModel == null ) { return ; } final Optional < Field > fieldOptional = getViewModelField ( view . getClass ( ) , viewModel . getClass ( ) ) ; if ( fieldOptional . isPresent ( ) ) { Field field = fieldOptional . get ( ) ; ReflectionUtils . accessMember ( field , ( ) -> { Object existingViewModel = field . get ( view ) ; if ( existingViewModel == null ) { field . set ( view , viewModel ) ; } } , "Can't inject ViewModel of type <" + viewModel . getClass ( ) + "> into the view <" + view + ">" ) ; } }
Injects the given viewModel instance into the given view. The injection
will only happen when the class of the given view has a viewModel field
that fulfills all requirements for the viewModel injection (matching
types, no viewModel already existing ...).

@param view
@param viewModel
Injects the given viewModel instance into the given view . The injection will only happen when the class of the given view has a viewModel field that fulfills all requirements for the viewModel injection ( matching types no viewModel already existing ... ) .