Example usage for javafx.fxml FXMLLoader FXMLLoader

List of usage examples for javafx.fxml FXMLLoader FXMLLoader

Introduction

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

Prototype

public FXMLLoader(URL location, ResourceBundle resources, BuilderFactory builderFactory,
        Callback<Class<?>, Object> controllerFactory) 

Source Link

Document

Creates a new FXMLLoader instance.

Usage

From source file:com.bekwam.mavenpomupdater.Main.java

@Override
public void start(Stage primaryStage) throws Exception {

    ////from   w  w  w.  ja  va2s  . c om
    // handle command line options
    //
    Application.Parameters params = getParameters();

    List<String> unnamedList = params.getUnnamed();

    Options options = new Options();
    options.addOption("help", false, "Print this message");
    options.addOption("hidpi", false, "Use high-DPI scaling");

    CommandLineParser p = new BasicParser();
    CommandLine cmd = p.parse(options, unnamedList.toArray(new String[0]));

    HelpFormatter formatter = new HelpFormatter();

    if (cmd.hasOption("help")) {
        if (log.isDebugEnabled()) {
            log.debug("[START] running as help command");
        }
        formatter.printHelp("Main", options);
        return;
    }

    AbstractModule module = null;
    if (runningAsJNLP()) {

        if (log.isInfoEnabled()) {
            log.info("using jnlp module and jnlp favorites store");
        }
        module = new MPUJNLPModule();
    } else {

        if (log.isInfoEnabled()) {
            log.info("using standalone module and in-memory favorites store");
        }
        module = new MPUStandaloneModule();
    }

    //
    // setup google guice
    //
    final Injector injector = Guice.createInjector(module);

    BuilderFactory builderFactory = new JavaFXBuilderFactory();

    Callback<Class<?>, Object> guiceControllerFactory = clazz -> injector.getInstance(clazz);

    //
    // setup icons
    //
    primaryStage.getIcons().add(new Image("images/mpu_icon_64.png"));

    //
    // load fxml and wire controllers
    //
    FXMLLoader mainViewLoader = new FXMLLoader(getClass().getResource("mavenpomupdater.fxml"), null,
            builderFactory, guiceControllerFactory);
    Parent mainView = mainViewLoader.load();
    MainViewController mainViewController = mainViewLoader.getController();

    FXMLLoader alertViewLoader = new FXMLLoader(getClass().getResource("alert.fxml"), null, builderFactory,
            guiceControllerFactory);
    Parent alertView = alertViewLoader.load();

    //
    // i'm continuing this manual wiring to 1) accommodate a potential
    // bi-directional reference problem and 2) to make sure that guice
    // doesn't return different object for the main -> alert and alert ->
    // main dependency injections
    //

    final AlertController alertController = alertViewLoader.getController();
    mainViewController.alertController = alertController;
    alertController.mainViewControllerRef = new WeakReference<MainViewController>(mainViewController);

    //
    // add FlowPane, StackPane objects (defined in program and outside of 
    // FXML)
    //
    final FlowPane fp = new FlowPane();
    fp.setAlignment(Pos.CENTER);
    fp.getChildren().add(alertView);
    fp.getStyleClass().add("alert-background-pane");

    final StackPane sp = new StackPane();
    sp.getChildren().add(fp); // initially hide the alert
    sp.getChildren().add(mainView);

    //
    // setup scene
    //
    Scene scene = new Scene(sp);
    scene.getStylesheets().add("com/bekwam/mavenpomupdater/mpu.css");

    scene.setOnKeyPressed(keyEvent -> {
        KeyCode key = keyEvent.getCode();
        if (key == KeyCode.ESCAPE && (sp.getChildren().get(1) == fp)) {
            if (log.isDebugEnabled()) {
                log.debug("[ESCAPE]");
            }
            alertController.action();
        }
    });

    //
    // setup stage
    //
    primaryStage.setTitle("Maven POM Version Updater");
    primaryStage.setScene(scene);

    if (cmd.hasOption("hidpi")) {

        if (log.isInfoEnabled()) {
            log.info("running in Hi-DPI display mode");
        }
        primaryStage.setWidth(2560.0);
        primaryStage.setHeight(1440.0);
        primaryStage.setMinWidth(1920.0);
        primaryStage.setMinHeight(1080.0);

        mainViewController.adjustForHiDPI();

    } else {
        if (log.isInfoEnabled()) {
            log.info("running in normal display mode");
        }

        primaryStage.setWidth(1280.0);
        primaryStage.setHeight(800.0);
        primaryStage.setMinWidth(1024.0);
        primaryStage.setMinHeight(768.0);

    }

    primaryStage.show();
}

From source file:de.micromata.mgc.javafx.ControllerService.java

public static <T extends Node, C> Pair<T, C> loadScene(String path, Class<T> nodeClass,
        Class<C> controlerClass) {
    try (InputStream is = ControllerService.class.getResourceAsStream(path)) {
        if (is == null) {
            throw new IllegalArgumentException("Canot find fxml file: " + path);
        }/*  w  w w  .  j  a  v a2s  .  c  o m*/
    } catch (IOException ex) {
        throw new IllegalArgumentException("Canot find fxml file: " + path);
    }
    ResourceBundle resbundle = I18NTranslations
            .asResourceBundle(MgcLauncher.get().getApplication().getTranslateService());
    FXMLLoader fxmlLoader = new FXMLLoader(ControllerService.class.getResource(path), resbundle, null,
            clazz -> {
                if (clazz.isAssignableFrom(controlerClass) == true) {
                    return PrivateBeanUtils.createInstance(controlerClass);
                } else {
                    return PrivateBeanUtils.createInstance(clazz);
                }

            });
    try {
        Object loaded = fxmlLoader.load();
        Object controler = fxmlLoader.getController();
        Validate.notNull(loaded);
        Validate.notNull(controler);
        return Pair.make((T) loaded, (C) controler);

    } catch (IOException ex) {
        throw new RuntimeIOException(ex);
    }
}

From source file:com.jf.javafx.Application.java

/**
 * Load a node and its controller.//from w  ww.  j  a va  2s. c  om
 * 
 * @param path
 * @return the node
 */
public Pair<Node, ?> createNode(String path) {
    final Application app = this;
    File fxml = getTemplateFile(path + ".fxml");
    Node node;
    Object controller = null;
    try {
        ResourceBundle bundle;
        try {
            bundle = getResourceBundle("controllers/" + path);
        } catch (Exception ex) {
            bundle = null;
        }

        FXMLLoader loader = new FXMLLoader(fxml.toURL(), bundle, new JavaFXBuilderFactory(),
                (Class<?> param) -> {
                    try {
                        Class cls = Controller.class;

                        if (cls.isAssignableFrom(param)) {
                            try {
                                return param.getConstructor(Application.class).newInstance(app);
                            } catch (Exception ex) {
                                Logger.getLogger(Application.class.getName()).log(Level.SEVERE, null, ex);
                                return param.newInstance();
                            }
                        } else {
                            return param.newInstance();
                        }
                    } catch (IllegalAccessException | IllegalArgumentException | InstantiationException
                            | SecurityException ex) {
                        MsgBox.showException(ex);
                        return null;
                    }
                });
        node = loader.load();
        controller = loader.getController();
    } catch (IOException ex) {
        MsgBox.showException(ex, "Error while navigate to path: " + path);

        // customize error screen
        AnchorPane p = new AnchorPane();

        //            StringWriter sw = new StringWriter();
        //            ex.printStackTrace(new PrintWriter(sw));
        //            p.getChildren().add(new Label(sw.toString()));

        node = p;
    }

    return new Pair(node, controller);
}