scalafxml icon indicating copy to clipboard operation
scalafxml copied to clipboard

HostServices as dependency

Open pacop opened this issue 9 years ago • 1 comments

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?

pacop avatar Feb 10 '17 09:02 pacop

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)
  }

pacop avatar Feb 10 '17 09:02 pacop