Examples of FXMLLoader


Examples of javafx.fxml.FXMLLoader

        final FXMLComponent annotation = targetClass.getAnnotation(FXMLComponent.class);
        if (annotation == null) {
            throw new IllegalStateException(String.format("No @FXMLComponent annotation could be retrieved from class %s.", targetClass.getName()));
        }
        final FXMLLoader fxmlLoader = new CdiFXMLLoader();
        CdiFXMLLoaderFactory.initializeFXMLLoader(
                fxmlLoader,
                targetClass,
                annotation.location(),
                annotation.resources(),
                annotation.charset());
        fxmlLoader.setRoot(target);
        fxmlLoader.setController(target);
        fxmlLoader.load();

    }
View Full Code Here

Examples of javafx.fxml.FXMLLoader

        this.bundle = getResourceBundle(bundleName);
        this.fxmlLoader = loadSynchronously(resource, bundle, conventionalName);
    }

    FXMLLoader loadSynchronously(final URL resource, ResourceBundle bundle, final String conventionalName) throws IllegalStateException {
        final FXMLLoader loader = new FXMLLoader(resource, bundle);
        loader.setControllerFactory((Class<?> p) -> Injector.instantiatePresenter(p));
        try {
            loader.load();
        } catch (IOException ex) {
            throw new IllegalStateException("Cannot load " + conventionalName, ex);
        }
        return loader;
    }
View Full Code Here

Examples of javafx.fxml.FXMLLoader

                location = new URL(locationString);
            } catch (final MalformedURLException e) {
                throw new RuntimeException(String.format("Cannot construct URL from string '%s'.", locationString), e);
            }
        }
        final FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(location);
        final String resourcesString = annotation.resources();
        if (!resourcesString.isEmpty()) {
            fxmlLoader.setResources(ResourceBundle.getBundle(resourcesString));
        }
        fxmlLoader.setCharset(Charset.forName(annotation.charset()));
        fxmlLoader.setController(instance);
        fxmlLoader.setRoot(instance);

        // Invoke "fxmlLoader.setTemplate(true)" if we are using JavaFX 8.0 or
        // higher to improve performance on objects that are created multiple times.
        try {
            final Method setTemplateMethod = FXMLLoader.class.getMethod("setTemplate", boolean.class);
            setTemplateMethod.invoke(fxmlLoader, Boolean.TRUE);
        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            // We simply ignore this exception. It means that we are using
            // a JavaFX runtime prior to JavaFX 8.0.
        }

        // Actual instantiation of the component has to happen on the JavaFX thread.
        // We simply delegate the loading.
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                try {
                    final Object loaded = fxmlLoader.load();
                    if (loaded != instance) {
                        throw new IllegalStateException("Loading of FXML component went terribly wrong! :-(");
                    }
                } catch (final IOException e) {
                    throw new RuntimeException(e);
View Full Code Here

Examples of javafx.fxml.FXMLLoader

    FXMLLoader createCdiFXMLLoader(final InjectionPoint injectionPoint) {

        final Annotated annotated = injectionPoint.getAnnotated();
        final Class<?> declaringClass = injectionPoint.getMember().getDeclaringClass();

        final FXMLLoader loader = new FXMLLoader() {
        };

        // Uses the currently loaded CDI implementation to look up controller classes
        // that have been specified via "fx:controller='...'" in our FXML files.
        loader.setControllerFactory((aClass) -> CDI.current().select(aClass));

        // If an annotation of type @FXMLLoaderParams can be found, use it's parameters
        // to configure the FXMLLoader instance that shall be used to perform the loading
        // of the FXML file.
        final FXMLLoaderParams fxmlLoaderParams = annotated.getAnnotation(FXMLLoaderParams.class);
        if (fxmlLoaderParams != null) {

            // Checks the location that has been specified (if any) and uses the default
            // class loader to create an URL that points to a FXML file on the classpath.
            final String location = fxmlLoaderParams.location();
            if (! location.equals(FXMLLoaderParams.LOCATION_UNSPECIFIED)) {
                loader.setLocation(declaringClass.getResource(location));
            }

            final String charset = fxmlLoaderParams.charset();
            if (! charset.equals(FXMLLoaderParams.CHARSET_UNSPECIFIED)) {
                loader.setCharset(Charset.forName(fxmlLoaderParams.charset()));
            }

            final String resources = fxmlLoaderParams.resources();
            if (!resources.equals(FXMLLoaderParams.RESOURCES_UNSPECIFIED)) {
                loader.setResources(ResourceBundle.getBundle(resources));
            }

        }

        return loader;
View Full Code Here

Examples of javafx.fxml.FXMLLoader

     */
    public Result load(final URL url, final ResourceBundle resources) throws IOException  {

        fxmlLoadingScope.enter(this);

        final FXMLLoader loader = new FXMLLoader();
        loader.setLocation(url);
        loader.setResources(resources);
        loader.setControllerFactory(new Callback<Class<?>, Object>() {
            @Override
            public Object call(Class<?> param) {
                // Use our Guice injector to fetch an instance of the desired
                // controller class
                return injector.getInstance(param);
            }
        });

        final Node root = (Node) loader.load(url.openStream());

        // Prepares the result that is being returned after loading the FXML hierarchy.
        final Result result = new Result();
        result.location.set(loader.getLocation());
        result.resources.set(loader.getResources());
        result.controller.set(loader.getController());
        result.root.set(root);
        result.charset.set(loader.getCharset());

        fxmlLoadingScope.exit();

        return result;

View Full Code Here

Examples of javafx.fxml.FXMLLoader

    public Stage get() {
        Stage dialog = new Stage(style);
        dialog.initModality(Modality.WINDOW_MODAL);
        dialog.initOwner(CustomerApp.getPrimaryStage());
        try {
            FXMLLoader loader = new FXMLLoader(fxml);
            loader.setControllerFactory(new Callback<Class<?>, Object>() {
                @Override
                public Object call(Class<?> aClass) {
                    return applicationContext.getBean(aClass);
                }
            });
            dialog.setScene(new Scene((Parent) loader.load()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return dialog;
    }
View Full Code Here

Examples of javafx.fxml.FXMLLoader

    public FXMLDialog(final DialogController controller, URL fxml, Window owner, StageStyle style) {
        super(style);
        initOwner(owner);
        initModality(Modality.WINDOW_MODAL);
        FXMLLoader loader = new FXMLLoader(fxml);
        try {
            loader.setControllerFactory(new Callback<Class<?>, Object>() {
                @Override
                public Object call(Class<?> aClass) {
                    return controller;
                }
            });
            controller.setDialog(this);
            setScene(new Scene((Parent) loader.load()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
View Full Code Here

Examples of javafx.fxml.FXMLLoader

     * @param <M> the model type that will manage this fxml node
     */
    @SuppressWarnings("unchecked")
    public static <M extends Model> FXMLComponent loadFXML(final M model, final String fxmlPath, final String bundlePath) {

        final FXMLLoader fxmlLoader = new FXMLLoader();

        // Use Custom controller factory to attach the root model to the controller
        fxmlLoader.setControllerFactory(new DefaultFXMLControllerBuilder(model));

        fxmlLoader.setLocation(convertFxmlUrl(model, fxmlPath));

        try {
            if (bundlePath != null) {
                fxmlLoader.setResources(ResourceBundle.getBundle(bundlePath));
            }
        } catch (final MissingResourceException e) {
            LOGGER.log(MISSING_RESOURCE_BUNDLE, e, bundlePath);
        }

        Node node = null;
        boolean error = false;
        try {
            error = fxmlLoader.getLocation() == null;
            if (error) {
                node = TextBuilder.create().text(FXML_ERROR_NODE_LABEL.getText(fxmlPath)).build();
            } else {
                node = (Node) fxmlLoader.load(fxmlLoader.getLocation().openStream());
            }

        } catch (final IOException e) {
            throw new CoreRuntimeException(FXML_NODE_DOESNT_EXIST.getText(fxmlPath), e);
        }

        final FXMLController<M, ?> fxmlController = (FXMLController<M, ?>) fxmlLoader.getController();

        // It's tolerated to have a null controller for an fxml node
        if (fxmlController != null) {
            // The fxml controller must extends AbstractFXMLController
            if (!error && !(fxmlLoader.getController() instanceof AbstractFXMLController)) {
                throw new CoreRuntimeException(BAD_FXML_CONTROLLER_ANCESTOR.getText(fxmlLoader.getController().getClass().getCanonicalName()));
            }
            // Link the View component with the fxml controller
            fxmlController.setModel(model);
        }

View Full Code Here

Examples of javafx.fxml.FXMLLoader

   *            the stream containing the document
   * @param closeStream
   *            close the stream after processing
   */
  public void load(InputStream fxml, boolean closeStream) throws Exception {
    FXMLLoader loader = new FXMLLoader();
    fxmlRoot = loader.load(fxml);
    if (closeStream)
      fxml.close();
  }
View Full Code Here

Examples of javafx.fxml.FXMLLoader

  @Test
  public void testLoading() throws Exception {
    InputStream fxml =  LanguagesBasicDemo.class.getResourceAsStream("LanguagesBasicDemoConfiguration.fxml");
    Assert.assertNotNull(fxml);
    FXMLLoader loader = new FXMLLoader();
    Object root = loader.load(fxml);
    Assert.assertNotNull(root);
   
   
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.