mgarin / weblaf

WebLaF is a fully open-source Look & Feel and component library written in pure Java for cross-platform desktop Swing applications.
http://weblookandfeel.com
GNU General Public License v3.0
1.14k stars 235 forks source link

addExtensions,Exception #548

Closed azwc closed 5 years ago

azwc commented 5 years ago

code from wiki page

<skin>

    <!-- Extension information -->
    <id>my.extension</id>
    <extends>weblaf.dark.skin</extends>

    <!-- Decorated frame -->
    <style type="rootpane" id="dark" extends="frame-decorated">
        <painter>
            <decorations>
                <decoration>
                    <WebShape round="15,0,15,0" />
                    <WebShadow type="outer" opacity="0.3" width="40" />
                    <LineBorder color="20,20,20" />
                    <ColorBackground color="68,68,68" />
                </decoration>
            </decorations>
        </painter>

        <!-- Frame buttons panel -->
        <style type="panel" id="buttons">

            <!-- Close button -->
            <style type="button" id="close">
                <painter>
                    <decorations>
                        <decoration>
                            <GradientBackground>
                                <color>140,50,50</color>
                                <color>110,50,50</color>
                            </GradientBackground>
                        </decoration>
                    </decorations>
                </painter>
            </style>

        </style>

        <!-- Content panel -->
        <style type="panel" id="content" extends="transparent" />

    </style>

</skin>
public class SimpleDecoration
{
    public static void main ( final String[] args )
    {
        // Installing L&F
        WebLookAndFeel.install ();

        // Installing our extension for default skin
        StyleManager.addExtensions ( new XmlSkinExtension ( SimpleDecoration.class, "SimpleExtension.xml" ) );

        // Creating decorated frame
        final WebFrame frame = new WebFrame ( StyleId.of ( "dark" ), "Dark frame" );
        frame.setDefaultCloseOperation ( WindowConstants.EXIT_ON_CLOSE );
        frame.setSize ( 400, 300 );
        frame.setLocationRelativeTo ( null );
        frame.setVisible ( true );
    }
}

exception message:

Exception in thread "main" java.lang.RuntimeException: Unable to deserialize object from XML
    at com.alee.utils.XmlUtils.fromXML(XmlUtils.java:502)
    at com.alee.managers.style.XmlSkinExtension.getMetaData(XmlSkinExtension.java:149)
    at com.alee.managers.style.XmlSkinExtension.<init>(XmlSkinExtension.java:95)
    at com.alee.managers.style.XmlSkinExtension.<init>(XmlSkinExtension.java:83)
    at com.github.sinyat.SimpleDecoration.main(SimpleDecoration.java:19)
Caused by: java.lang.RuntimeException: Unable to read XML file 'SimpleExtension.xml' near class: com.github.sinyat.SimpleDecoration
    at com.alee.utils.XmlUtils.fromXML(XmlUtils.java:475)
    ... 4 more

What can I do to solve this exception?

mgarin commented 5 years ago

This line from exception:

Caused by: java.lang.RuntimeException: Unable to read XML file 'SimpleExtension.xml' near class: com.github.sinyat.SimpleDecoration

Basically says that your SimpleExtension.xml file cannot be found near the SimpleDecoration class. It has to be located in the very same package to retrieve it as a resource. If it's not - you will receive that exception.

But if you did place it in the SimpleDecoration class package and are still getting that error - there might be some issues with your IDE actually copying the XML resource to the output before running the application, try rebuilding your project/checking IDE configuration for XML to be considered a resource.

azwc commented 5 years ago

thanks,after your prompt, the problem has been solved.

add the following configuration: pom.xml

<build>
        <resources>
            <resource>
                <directory>${basedir}/src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
            </resource>
        </resources>
    </build>
mgarin commented 5 years ago

Glad it helped :) Overall it's not necessary to use exactly that XmlSkinExtension constructor - you can have extension/skin files located anywhere, but you'll need to provide a correct path to them in that case.

azwc commented 5 years ago

Glad it helped :) Overall it's not necessary to use exactly that XmlSkinExtension constructor - you can have extension/skin files located anywhere, but you'll need to provide a correct path to them in that case.

I also tried to put the xml file in src/main/resources, but I found that the XmlSkinExtension class path parameter does not support the use of "classpath"

mgarin commented 5 years ago

Some pathing options are indeed inconvenient (or don't work the way you would probably expect them to), but I do have some plans to improve them in future updates along with some other QoL changes.