scalafxml
scalafxml copied to clipboard
HostServices as dependency
Hi, I was trying to share the instance of hostServices with one of the my controllers, but I am getting constantly a nullPointerException
object Main extends JFXApp {
val resource = getClass.getResource("/Views/main.fxml")
val i18n = new PropertyResourceBundle(getClass.getResource("/i18n/en.properties").openStream)
val my: HostServices = hostServices
val loader = new FXMLLoader(resource, new DependenciesByType(Map(
typeOf[HostServices] -> my))) {
setResources(i18n)
}
val root = loader.load[javafx.scene.Parent]
val controller = loader.getController[UnitController]
stage = new PrimaryStage() {
title = "Test"
scene = new Scene(root)
}
In MainController:
@sfxml
class MainController(val hostServices: HostServices) extends UnitController {
println("HostServices: " + (hostServices.delegate== null))
}
}
Although in MainController hostServices is NOT null, hostServices.delegate is null. Any idea?
If I create the following hack, and it works. BTW, pretty dirty solution.
class P(var my: HostServices = null) {}
val loader = new FXMLLoader(resource, new DependenciesByType(Map(
typeOf[P] -> new P(hostServices)))) {
setResources(i18n)
}