Last active
January 20, 2021 16:40
-
-
Save tarrsalah/5437723 to your computer and use it in GitHub Desktop.
Generic FXMLLoader.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.net.URL; | |
import javafx.fxml.FXMLLoader; | |
import javafx.util.Callback; | |
/** | |
* InjectingFXMLLoader.java (UTF-8) | |
* | |
* Apr 13, 2013 | |
* @author tarrsalah.org | |
*/ | |
public class InjectingFXMLLoader { | |
/** | |
* | |
* @param <N> | |
* @param injector | |
* @return a controller injected within an FXML. | |
*/ | |
public static <N> N loadFXMLController(final N controller, URL url) throws IOException { | |
FXMLLoader loader= new FXMLLoader(); | |
loader.setLocation(url); | |
loader.setControllerFactory(new Callback<Class<?>, Object>() { | |
public Object call(Class<?> p) { | |
return controller; | |
} | |
}); | |
loader.load(); | |
return loader.getController(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do you use it