title: JavaFX: Need help understanding setControllerFactory
body: <p>I set up multiple custom controllers during the creation of an app and would need some help in organizing these controllers with setControllerFactory in JavaFX.</p>
  
  <p>I'm fairly inexperienced with JavaFX but invested quite some time in creating a small app with Scenebuilder and JavaFX.</p>
  
  <p>Background of the app
  The app consists of: 
   - a map (implemented as an imageView)
   - a sidebar with buttons and icons for drag and drop events. 
   - the map also has separate layers as the target for the drag and drop of different icon types.</p>
  
  <p>As a prototype of my drag and drop event I used the instructions of Joel Graff (<a href="https://monograff76.wordpress.com/2015/02/17/developing-a-drag-and-drop-ui-in-javafx-part-i-skeleton-application/" rel="nofollow noreferrer">https://monograff76.wordpress.com/2015/02/17/developing-a-drag-and-drop-ui-in-javafx-part-i-skeleton-application/</a>). He writes "in order for an object to be visible beyond a container’s edges, it must be a child of a parent or other ancestral container – it must belong to a higher level of the hierarchy. In the case of our drag-over icon, this means we had to add it as a child to the RootLayout’s top-level AnchorPane." and he uses dynamic roots for his project. </p>
  
  <p>To teach myself how to use custom control with FXML I used Irina Fedortsova's tutorial <a href="https://docs.oracle.com/javafx/2/fxml_get_started/custom_control.htm" rel="nofollow noreferrer">https://docs.oracle.com/javafx/2/fxml_get_started/custom_control.htm</a>. 
  And to learn how to set up multiple screens I used the video <a href="https://www.youtube.com/watch?v=5GsdaZWDcdY" rel="nofollow noreferrer">https://www.youtube.com/watch?v=5GsdaZWDcdY</a> and associating code from <a href="https://github.com/acaicedo/JFX-MultiScreen" rel="nofollow noreferrer">https://github.com/acaicedo/JFX-MultiScreen</a>.</p>
  
  <p>After building my app the logic tier of my app got﻿ more and more entangled with the presentation tier, and I feel as if my code would benefit greatly from some refactoring.
  My problem seems to be a lack in the understanding of the load and initialize process of controller classes. Since the drag icons and the RootLayout have to be loaded from the beginning, it is a mystery to me how I can load these classes in a way that I can call them again at a later time.</p>
  
  <p>When I was looking for further solutions, I repeatedly came across the method setControllerFactory. Unfortunately I can't find a good explanation for how to use it properly and what it's specific purpose is. 
  The only tutorial I found was: <a href="https://riptutorial.com/javafx/example/8805/passing-parameters-to-fxml---using-a-controllerfactory" rel="nofollow noreferrer">https://riptutorial.com/javafx/example/8805/passing-parameters-to-fxml---using-a-controllerfactory</a>, unfortunately it seems to be a bit insufficient for my purpose.</p>
  
  <p>I feel as if I would benefit the most from a methode/class with which I could organize all my custom controllers, load and initialize them at the appropriate time and then later access them again (similar to the interface and superclass in the video for JFX-MultiScreen).</p>