javaxmlloadstylessynth

How to load a synth style in java


I am experimenting with synth styles and i came across the following problem for which a haven't found an answer yet after quite some research. I have the following xml file

<?xml version="1.0" encoding="UTF-8"?>
<synth>
    <style id="button">
        <state>
            <color value="blue" type="BACKGROUND"/>
            <color value="yellow" type="FOREGROUND"/>
        </state>
    </style>
    <bind style="button" type="region" key="Button"/>
</synth>

and the following java code to load that xml file as a SynthLookAndFeel object

private void initializeStyle() {
    SynthLookAndFeel laf = new SynthLookAndFeel();
    try {
        laf.load(this.getClass().getResourceAsStream("Style.xml"), this.getClass());
        UIManager.setLookAndFeel(laf);
    } catch (ParseException | UnsupportedLookAndFeelException e) {
        e.printStackTrace();
    }
}

When i try to run this code i get the following exception

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: You must supply an InputStream, StyleFactory and Class or URL

Any suggestions on how to fix this are more than welcome.


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 some images:

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

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

    I hope this can help you.