This control makes use of LoaderUtils and it's capable of loading multiple FXML views with ExecutorServices.
The informations about a view are stored in bean classes, MFXLoaderBean. Those can be added to this loader
by using the various "addView(...)" methods, and will be stored in a Map, every bean is associated to a String,
which should be the identifier of the view, you can also use LoaderUtils.generateKey(URL) to automatically generate a key.
start() or startWith(ExecutorService).
After all views have been loaded the onLoaded(List) method is called, see also setOnLoadedAction(Consumer).
1) To load FXML files it uses a FXMLLoader of course. But, in some cases (like DI framework) you want to use
a specific FXMLLoader, so the loader creates them with a Supplier, see fxmlLoaderSupplierProperty().
The default supplier is just "FXMLLoader::new", but you can easily change that if you need to.
2) To make the views ready for switching the loader can "preload" them (compute both CSS and Layout), you can
manage this behavior by setting the "cache level", see setCacheLevel(LoaderCacheLevel) and LoaderCacheLevel
NOTE: the cache level must be set before invoking the start() method.
By default it is set to: LoaderCacheLevel.SCENE_CACHE
- See Also:
-
Property Summary
PropertiesTypePropertyDescriptionSpecifies theSupplierused to build a newFXMLLoadereach time a view has to be loaded. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddView(MFXLoaderBean bean) Adds the given view to the views map.Builds a newMFXLoaderBeanwith the given identifier and FXML file, then adds it to the views map.Builds a newMFXLoaderBeanwith the given identifier, FXML file and controller factory, then adds it to the views map.Specifies theSupplierused to build a newFXMLLoadereach time a view has to be loaded.Gets the value of thefxmlLoaderSupplierproperty.protected voidonLoaded(List<MFXLoaderBean> beans) This method is called once all the views have been loaded bystart()orstartWith(ExecutorService).setCacheLevel(LoaderCacheLevel cacheLevel) Sets theLoaderCacheLevelfor this loader.setFxmlLoaderSupplier(Supplier<FXMLLoader> fxmlLoaderSupplier) setOnLoadedAction(Consumer<List<MFXLoaderBean>> onLoadedAction) Sets the action to perform once all the views have been loaded.voidstart()Starts the loading process by collecting all the views from the map that are still not loaded.voidstartWith(ExecutorService executorService) Same asstart()but the load tasks are submitted to the givenExecutorService.
-
Property Details
-
fxmlLoaderSupplier
Specifies theSupplierused to build a newFXMLLoadereach time a view has to be loaded.
-
-
Constructor Details
-
MFXLoader
public MFXLoader() -
MFXLoader
-
-
Method Details
-
start
public void start()Starts the loading process by collecting all the views from the map that are still not loaded. Then for eachMFXLoaderBeanbuilds theCallableused to load the FXML root, seebuildTask(MFXLoaderBean), and sends it to theLoaderUtils's executor, to load the view usesFuture.get(). Once the fxml has been loaded invokescacheParent(Parent)then finally increments the number that keeps track of how many views have been loaded.At the end of the loop calls
onLoaded(List), the list is given byMap.values()(wrapped in an ArrayList). -
startWith
Same asstart()but the load tasks are submitted to the givenExecutorService. -
addView
Adds the given view to the views map. -
addView
Builds a newMFXLoaderBeanwith the given identifier and FXML file, then adds it to the views map. -
addView
public MFXLoader addView(String viewName, URL fxmlFile, Callback<Class<?>, Object> controllerFactory) Builds a newMFXLoaderBeanwith the given identifier, FXML file and controller factory, then adds it to the views map. -
getView
- Returns:
- a view for the given identifier, or null if no view is found
-
onLoaded
This method is called once all the views have been loaded bystart()orstartWith(ExecutorService).This simple methods is just responsible for executing the action specified by the user,
setOnLoadedAction(Consumer), if not null. -
getFxmlLoaderSupplier
Gets the value of thefxmlLoaderSupplierproperty.- Property description:
- Specifies the
Supplierused to build a newFXMLLoadereach time a view has to be loaded. - Returns:
- the value of the
fxmlLoaderSupplierproperty - See Also:
-
fxmlLoaderSupplierProperty
Specifies theSupplierused to build a newFXMLLoadereach time a view has to be loaded.- Returns:
- the
fxmlLoaderSupplierproperty - See Also:
-
setFxmlLoaderSupplier
-
getOnLoadedAction
-
setOnLoadedAction
Sets the action to perform once all the views have been loaded.The action is a
Consumerwhich carries the list fo loaded views. -
getCacheLevel
-
setCacheLevel
Sets theLoaderCacheLevelfor this loader.
-