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);
fxmlLoader.setBuilderFactory(injector.getInstance(FXMLComponentBuilderFactory.class));
// 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);