Busy popup in JavaFX when FXML parsing is too slow

695 Views Asked by At

FXML performance is an issue with JavaFX, if you have complex screens and have divided them into small components (for maintainability / reuse) that use FXML, then it can get really slow.

As FXML parsing is made in UI Thread (not sure of this, still it blocks the JavaFX Application Thread), you cannot show a glasspane / popup / etc in JavaFX when FXML is being processed.

The only workaround I found is to use a Swing popup (as it is in Swing UI Thread, you can still show something) to provide a feedback to the user (it is working / not a bug / wait a little more) when FXML is being loaded and to close it when no more FXML files are parsed.

I have built a facade above FXMLLoader to do so. Also this also works with OpenGL libraries as well (LWJGL for instance, instead of Swing, anything that is UI and is not in JavaFX Application Thread works).

I was wondering if a better solution exists (JavaFX only, not mixing UI frameworks) as this artificially adds complexity to the project and won't be ported well with OpenJFX ports.

1

There are 1 best solutions below

2
On BEST ANSWER

Recommended Solution

Use JavaFX 8u40+, you can find an early access release.

For 8u40, the following bug was fixed:

This fix allows you to create all controls (except currently a WebView) off of the JavaFX application thread. That means you can load up your FXML asynchronously to the JavaFX application thread within a standard JavaFX Task. While the task is running you can have a please wait dialog or animated progress indicator displayed or whatever you like (in JavaFX, no need to use other frameworks like Swing/LWJGL).

My favorite way of handling this is to load the FXML elements up while the user is shown a login prompt or needs to create some input (but whether or not you use the "load stuff in the background while awaiting user input" trick is app dependent).

You can also load your FXML in the init function of your application, so that the FXML is loaded in parallel to the JavaFX system starting up (you need to take care around threading a bit for that to ensure that you don't actually try to show your scene until all the FXML is loaded and the operation to show the scene occurs on the JavaFX application thread).

Alternate Solution

You could also try this solution to Convert FXML to Java as part of the build, then perhaps there won't be any issue with slow loading of FXML (because there is no longer any FXML, it has been converted to Java). But I don't know if that solution is currently mature and stable enough for your purposes.