Example usage for javafx.fxml FXMLLoader setControllerFactory

List of usage examples for javafx.fxml FXMLLoader setControllerFactory

Introduction

In this page you can find the example usage for javafx.fxml FXMLLoader setControllerFactory.

Prototype

public void setControllerFactory(Callback<Class<?>, Object> controllerFactory) 

Source Link

Document

Sets the controller factory used by this loader.

Usage

From source file:caillou.company.clonemanager.gui.spring.SpringFxmlLoader.java

public static LoadingMojo load(String url, final String controllerClassName) {

    lastLoadedURL = url;/*ww w. j  a v  a2 s  .  c  o m*/
    FXMLLoader loader = new FXMLLoader(MainApp.class.getResource(url), resourceBundle);
    loader.setControllerFactory(new Callback<Class<?>, Object>() {
        @Override
        public Object call(Class<?> clazz) {
            if (controllerClassName != null) {
                try {
                    return applicationContext.getBean(Class.forName(controllerClassName));
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(SpringFxmlLoader.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            return applicationContext.getBean(clazz);
        }
    });

    try {
        Parent parent = loader.load();
        LoadingMojo loadingMojo = new LoadingMojo();
        loadingMojo.setController(loader.getController());
        loadingMojo.setParent(parent);
        return loadingMojo;
    } catch (IOException ex) {
        Logger.getLogger(SpringFxmlLoader.class.getName()).log(Level.SEVERE, null, ex);
    } catch (Exception exMain) {
        System.out.println("Error");
    }
    return null;
}

From source file:at.ac.tuwien.qse.sepm.gui.App.java

@Override
public void start(Stage stage) throws Exception {
    logger.info("Application started.");

    FXMLLoader loader = new FXMLLoader();
    loader.setControllerFactory(context::getBean);

    // set base location so that resources can be loaded using relative paths
    loader.setLocation(getClass().getClassLoader().getResource("view"));

    Parent root = loader.load(getClass().getClassLoader().getResourceAsStream("view/Main.fxml"));

    stage.setScene(new Scene(root));

    stage.setTitle("travelimg");
    stage.getIcons().add(getApplicationIcon());
    stage.show();//from   w w w . j a v a 2  s  . c o  m

    PhotoService photoService = (PhotoService) context.getBean("photoService");
    photoService.initializeRepository();
}

From source file:dtv.DTVEdit.java

private Object load(String url) {
    FXMLLoader loader = new FXMLLoader();
    loader.setControllerFactory(clazz -> applicationContext.getBean(clazz));
    loader.setLocation(DTVEdit.class.getResource(url));

    try {//  ww  w  . j  a  va 2s  . co  m
        return loader.load();
    } catch (IOException e) {
        LOG.error("Some bad things!", e);
    }

    return null;
}

From source file:de.tmosebach.mm.jfxui.SpringFXMLLoader.java

public Node load(URL url) {
    try {/*w ww .ja  v  a  2s.  c  o m*/
        FXMLLoader loader = new FXMLLoader(url);
        loader.setControllerFactory(new Callback<Class<?>, Object>() {
            @Override
            public Object call(Class<?> aClass) {
                return context.getBean(aClass);
            }
        });
        return loader.load();
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(String.format("Failed to load FXML file '%s'", url));
    }
}

From source file:ninja.eivind.hotsreplayuploader.di.HotSReplayUploaderConfiguration.java

@Bean
public FXMLLoaderFactory fxmlLoader(ControllerFactory controllerFactory, BuilderFactory builderFactory) {
    return () -> {
        FXMLLoader loader = new FXMLLoader();
        loader.setControllerFactory(controllerFactory);
        loader.setBuilderFactory(builderFactory);
        return loader;
    };/*w w  w.j  a  va  2 s.  c om*/
}

From source file:com.fishbeans.app.FXMLComponentBase.java

private FXMLLoader doLoad(URL fxml) throws IllegalStateException {

    FXMLLoader loader = new FXMLLoader(fxml);
    loader.setControllerFactory(this::createControllerAsBean);

    try {//w ww . j av a 2 s.c o m
        loader.load();
    } catch (IOException ex) {
        throw new IllegalStateException(ex);
    }

    return loader;
}

From source file:viewfx.view.support.fxml.FxmlViewLoader.java

public View loadFromFxml(URL fxmlUrl) {
    checkNotNull(fxmlUrl, "FXML URL must not be null");
    try {/*  w  w  w  .  j av  a2  s. co m*/
        FXMLLoader loader = new FXMLLoader(fxmlUrl, resourceBundle);
        loader.setControllerFactory(viewFactory);
        loader.load();
        Object controller = loader.getController();
        if (controller == null)
            throw new ViewfxException("Failed to load view from FXML file at [%s]. "
                    + "Does it declare an fx:controller attribute?", fxmlUrl);
        if (!(controller instanceof View))
            throw new ViewfxException(
                    "Controller of type [%s] loaded from FXML file at [%s] "
                            + "does not implement [%s] as expected.",
                    controller.getClass(), fxmlUrl, View.class);
        return (View) controller;
    } catch (IOException ex) {
        throw new ViewfxException(ex, "Failed to load view from FXML file at [%s]", fxmlUrl);
    }
}

From source file:org.pieShare.pieShareAppFx.springConfiguration.PieShareAppFx.java

@Bean
@Lazy//from   w  w w  . j  av a2  s  .co m
@Scope(value = "prototype")
public FXMLLoader fxmlLoader() {
    FXMLLoader loader = new FXMLLoader();
    loader.setControllerFactory(controllerFactory());
    return loader;
}

From source file:com.sunkur.springjavafxcontroller.screen.ScreensContoller.java

public void loadScreen(String fxml) {
    BaseScreenController oldScreenController = this.getCurrentController();
    try {/*  ww w . ja  va 2  s  .com*/

        Class controllerClass = FXMLUtils.getControllerClass(fxml);
        final BaseScreenController fxmlController = (BaseScreenController) applicationContext
                .getBean(controllerClass);

        if (this.screens.get(fxmlController.getScreenId()) == null) {
            FXMLLoader loader = new FXMLLoader(getClass().getResource(fxml));
            loader.setControllerFactory(new Callback<Class<?>, Object>() {
                @Override
                public Object call(Class<?> aClass) {
                    return fxmlController;
                }
            });
            Parent root = loader.load();
            fxmlController.setRoot(root);
            this.screens.put(fxmlController.getScreenId(), fxmlController);
        }

        this.currentScreenId = fxmlController.getScreenId();
        swapScreen(getCurrentController().getRoot());
        if (oldScreenController != null) {
            if (oldScreenController.getClass().isAnnotationPresent(ScreenScoped.class)) {
                this.screens.remove(oldScreenController.getScreenId());
                this.screenScope.remove(oldScreenController.getScreenId());
            }
        }

    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.kordamp.javatrove.example04.view.AppView.java

public Scene createScene() {
    String basename = getClass().getPackage().getName().replace('.', '/') + "/app";
    URL fxml = getClass().getClassLoader().getResource(basename + ".fxml");
    FXMLLoader fxmlLoader = new FXMLLoader(fxml);
    fxmlLoader.setControllerFactory(param -> AppView.this);
    Parent root = null;/* w  w w  . j a v  a2 s  . co m*/
    try {
        root = (Parent) fxmlLoader.load();
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }

    organization.textProperty().addListener((observable, oldValue, newValue) -> {
        model.setState(isBlank(newValue) ? DISABLED : READY);
    });

    model.stateProperty().addListener((observable, oldValue, newValue) -> Platform.runLater(() -> {
        switch (newValue) {
        case DISABLED:
            enabled.setValue(false);
            running.setValue(false);
            break;
        case READY:
            enabled.setValue(true);
            running.setValue(false);
            break;
        case RUNNING:
            enabled.setValue(false);
            running.setValue(true);
            break;
        }
    }));

    ObservableList<Repository> items = createJavaFXThreadProxyList(model.getRepositories().sorted());

    repositories.setItems(items);
    EventStreams.sizeOf(items).subscribe(v -> total.setText(String.valueOf(v)));
    organization.textProperty().bindBidirectional(model.organizationProperty());
    bindBidirectional(limit.textProperty(), model.limitProperty(), new NumberStringConverter());
    loadButton.disableProperty().bind(Bindings.not(enabled));
    cancelButton.disableProperty().bind(Bindings.not(running));
    progress.visibleProperty().bind(running);

    Scene scene = new Scene(root);
    scene.getStylesheets().addAll(basename + ".css", "bootstrapfx.css");
    return scene;
}