javaswinglook-and-feeljava-modulesynth

How to use SynthLookAndFeel in a modular java application?


I'm trying to use the SynthLookAndFeel class but cannot make it work in my modular application.

I went back to basics with the official Synth Application sample from the Java Tutorial page on The Synth Look And Feel, and just added a module-info.java file with the following contents:

module SynthApplication {
    requires java.desktop;
}

It builds OK, but when run, the console shows a NullPointerException stack trace pointing to java.desktop/sun.awt.SunToolkit.getImageFromHash(SunToolkit.java:653), and only a blank window is displayed.

The issue seems to lie in the resolution of the images (e.g. images/button.png) that are referenced in the buttonSkin.xml definition of the L&F: Image paths are converted to a pseudo-package name (lookandfeel.images) which is not exported to the java.desktop module.

I tried to add exports lookandfeel; to module-info.java => same error

I tried to add exports lookandfeel.images; to module-info.java => build error Error:(4, 24) java: package is empty or does not exist: lookandfeel.images

Is there a way to make the image resolution work or is SynthLookAndFeel purely incompatible with java modules ?

Thanks. Vicne.

Note: I'm using openJdk 14


Solution

  • OK, after more code digging in the JDK, I discovered that it tested if the module was "open" (aka open to introspection). So I tried to declare the module as "open" as follows:

    open module SynthApplication {
        requires java.desktop;
    }
    

    And woo-hoo it worked.

    OK, learned something: SynthLookAndFeel must be part of an open module to work.

    Hope it can help others.