Building Images in FXML

Emilia López emilia.lopezf.1999 at gmail.com
Fri Jun 21 18:54:53 UTC 2024


Hi :)

I am currently working on an app that loads its layouts from FXML,
everything is working great so far* and I am somewhat impressed by the
myriad of things you can define in that format.

*However I have encountered one issue having ImageViews with Images
declared in the FXML, everything works great until I try to define one
of the Image properties (for the `@NamedArg`uments) as a nested XML
attribute, in my case I want to bring the Image URL from a constant:

<ImageView>
    <Image preserveRatio="true" smooth="true">
        <url>
            <PngPaths fx:constant="EDIT_NOTE"/>
        </url>
    </Image>
</ImageView>

I get the following error (JavaFX 17.0.11):

javafx.fxml.LoadException:
file:/home/emilia/projects/java/planta-produccion/planta-produccion/build/libs/planta-produccion-1.13-SNAPSHOT.jar!/fxml/Primary.fxml:261
        at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2714)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2692)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2555)
        at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2523)
        at ar.emily.plantaproduccion.controller.Controller.load(Controller.java:24)
        ... 11 more
    Caused by: java.lang.UnsupportedOperationException
        at javafx.fxml/com.sun.javafx.fxml.builder.JavaFXImageBuilder.entrySet(JavaFXImageBuilder.java:77)
        at java.base/java.util.AbstractMap.containsKey(AbstractMap.java:144)
        at javafx.fxml/javafx.fxml.FXMLLoader$PropertyElement.<init>(FXMLLoader.java:1381)
        at javafx.fxml/javafx.fxml.FXMLLoader.createElement(FXMLLoader.java:2869)
        at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2842)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2641)
        ... 14 more

My workaround for that was to have a custom ImageBuilder and a
BuilderFactory for it that I add to the FXMLLoader, but this also
raises a problem with IntelliJ IDEA's Scene Builder integration, since
I can't add those to its FXMLLoader it will fail to load the layouts
with essentially the same error.

I was wondering if someone knows/is aware about this and if there's a
better approach or solution than a custom builder, (granted that
JavaFXImageBuilder is basically just a Map, but having a custom
builder also means that I will miss out on future changes to the
default image builder unless I check for them whenever we upgrade
JDK/JFX versions).

Cheers,
Emilia López.


More information about the openjfx-dev mailing list