javaswingjslidersynth

How to use SynthLookAndFeel with xml file, where xml file path will load the file using getResourceAsStream method?


I am using

SynthLookAndFeel laf = new SynthLookAndFeel();
laf.load(MyJSlider.class.getResourceAsStream("/ui/demo.xml"), MyJSlider.class);      
//laf.load(new URL("file:///var/tmp/demo.xml"));

Where my demo.xml has following:

<style id="SliderTrackStyle">
    <opaque value="TRUE"/>
    <state>
      <imagePainter method="SliderTrackBackground" path="/var/tmp/JavaUnitTest/src/image/menu/bg.jpg" sourceInsets="0 0 0 0" />
    </state>
</style>
<bind style="SliderTrackStyle" type="region" key="SliderTrack" />

Causes error:

run-single:
Uncaught error fetching image:
java.lang.NullPointerException
    at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:115)
    at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:125)
    at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:263)
    at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:205)
    at sun.awt.image.ImageFetcher.run(ImageFetcher.java:169)
BUILD STOPPED (total time: 9 seconds)

To avoid and to have less risks.

How can i use it like path=Menu.class.getResourceAsStream("/image/menu/bg.jpg") where i wanted to avoid using the static path shown in example.

Follow up:

enter image description here


Solution

  • You can find a good example in Advanced Synth.

    You can see in the example that:

    SynthLookAndFeel synth = new SynthLookAndFeel();
    synth.load(SynthFrame.class.getResourceAsStream("demo.xml"), SynthFrame.class);
    UIManager.setLookAndFeel(synth);
    

    And demo.xml lives in demo.synth package, the same of SynthFrame class, where this XML contains:

    <imageIcon id="check_off" path="images/checkbox_off.png"/>
    

    Where checkbox_off.png lives in demo.synth.images package.