Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

ControllerFactory in FXMLLoader not being called when loading from uri

1006513May 1 2013 — edited May 4 2013
Im trying to use the setControllerFactory() method in FXMLLoader for dependency injection, but stumbled upon an issue that looks like a bug to me. The controller factory works fine when loading the fxml from an input stream, but does not get invoked when loading from an uri. I wrote a simple unit test to demonstrate the issue:
public class FXMLLoaderTest {

	private FXMLLoader loader;

	@Test
	public void testFXMLFactory() throws IOException {
		FXMLLoader loader = new FXMLLoader();
		Callback<Class<?>, Object> callback = new Callback<Class<?>, Object>() {
			@Override
			public Object call(Class<?> aClass) {
				System.out.println("Controller factory called");
				return new String("Works");
			}
		};
		loader.setControllerFactory(callback);
		
		// This makes the test fail
		//Object node = loader.load(getClass().getClassLoader().getResource("controllerTest.fxml"));
		
		// While this works as expected
		Object node = loader.load(getClass().getClassLoader().getResourceAsStream("controllerTest.fxml"));

		assertThat(loader.getController(), is(equalTo((Object) "Works")));
	}
}
Im using javafx 2.2 shipped with JDK 7u21. Am I missing something or should I file an issue for this?
This post has been answered by James_D on May 1 2013
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 1 2013
Added on May 1 2013
2 comments
352 views