load JavaFX FXML - Java JavaFX

Java examples for JavaFX:FXML

Description

load JavaFX FXML

Demo Code


    //package com.java2s;
    import javafx.fxml.FXMLLoader;

    import javafx.scene.Parent;

    import java.io.IOException;

    public class Main {
        private static final String resourcePath = "%s.fxml";

        public static Parent loadFXML(Object controller) {

    FXMLLoader fxmlLoader = new FXMLLoader(controller.getClass().getResource(getViewPath(controller.getClass())));
    fxmlLoader.setControllerFactory((c) -> controller );
    fxmlLoader.setRoot(controller);//from   www.  j a v a2s .c  o  m

    try {
        fxmlLoader.load();
        return fxmlLoader.getRoot();
    } catch (IOException exception) {
        throw new RuntimeException(exception);
    }

}

        private static String getViewPath(Class clazz) {
            return String.format(resourcePath, clazz.getSimpleName());
        }
    }

Related Tutorials