Setting a custom skin on an FxButton control in Flex Gumbo

by Peter deHaan on March 4, 2009

in FxButton, beta

The following example shows how you can set a custom skin on an Flex Gumbo FxButton control by setting the skinClass style.

Full code after the jump.

To use the following code, you must have Flash Player 10 and a Flex Gumbo SDK installed in your Flex Builder 3. For more information on downloading and installing the Gumbo SDK into Flex Builder 3, see “Using the beta Gumbo SDK in Flex Builder 3″.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/04/setting-a-custom-skin-on-an-fxbutton-control-in-flex-gumbo/ -->
<FxApplication name="FxButton_skinClass_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        backgroundColor="white">
    <layout>
        <BasicLayout />
    </layout>

    <FxButton label="FxButton"
            horizontalCenter="0"
            verticalCenter="0"
            skinClass="MyExternalSkin" />

</FxApplication>

View MyExternalSkin.mxml

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/04/setting-a-custom-skin-on-an-fxbutton-control-in-flex-gumbo/ -->
<Skin xmlns="http://ns.adobe.com/mxml/2009" resizeMode="scale">

    <states>
        <State name="up"/>
        <State name="over"/>
        <State name="down"/>
        <State name="disabled"/>
    </states>

    <Metadata>[HostComponent("mx.components.FxButton")]</Metadata>

    <Path data.up="M 0 0 L 10 0 L 10 10 L 0 10 Z"
            data.over="M 0 0 L 20 0 L 20 20 L 0 20 Z"
            data.down="M 0 0 L 30 0 L 30 30 L 0 30 Z">
        <fill>
            <SolidColor color="#000000"/>
        </fill>
    </Path>

</Skin>

You can also set the skinClass style in an external .CSS file or <Style/> block, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/04/setting-a-custom-skin-on-an-fxbutton-control-in-flex-gumbo/ -->
<FxApplication name="FxButton_skinClass_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        backgroundColor="white">
    <layout>
        <BasicLayout />
    </layout>

    <Style>
        FxButton {
            skinClass: ClassReference("MyExternalSkin");
        }
    </Style>

    <FxButton label="FxButton"
            horizontalCenter="0"
            verticalCenter="0" />

</FxApplication>

Or, you can set the skinClass style using ActionScript, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/04/setting-a-custom-skin-on-an-fxbutton-control-in-flex-gumbo/ -->
<FxApplication name="FxButton_skinClass_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        backgroundColor="white">
    <layout>
        <BasicLayout />
    </layout>

    <Script>
        <![CDATA[
            private function btn_click(evt:Event):void {
                btn.setStyle("skinClass", MyExternalSkin);
            }
        ]]>
    </Script>

    <FxButton id="btn"
            label="FxButton"
            horizontalCenter="0"
            verticalCenter="0"
            click="btn_click(event);" />

</FxApplication>

Finally, you can also define the skin directly in the MXML file by wrapping the Skin object in a <Component/> tag within the <Declarations/> section, as seen in the following example (big thanks to my co-worker Ryan Frishberg for the heads up on this solution):

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/04/setting-a-custom-skin-on-an-fxbutton-control-in-flex-gumbo/ -->
<FxApplication name="FxButton_skinClass_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        backgroundColor="white">
    <layout>
        <BasicLayout />
    </layout>

    <Declarations>
        <Component className="MyInlineSkin">
            <Skin xmlns="http://ns.adobe.com/mxml/2009" resizeMode="scale">
                <states>
                    <State name="up" />
                    <State name="over" />
                    <State name="down" />
                    <State name="disabled"/>
                </states>

                <Metadata>[HostComponent("mx.components.FxButton")]</Metadata>

                <Path data.up="M 0 0 L 10 0 L 10 10 L 0 10 Z"
                        data.over="M 0 0 L 20 0 L 20 20 L 0 20 Z"
                        data.down="M 0 0 L 30 0 L 30 30 L 0 30 Z">
                    <fill>
                        <SolidColor color="#000000"/>
                    </fill>
                </Path>
            </Skin>
        </Component>
    </Declarations>

    <FxButton label="FxButton"
            horizontalCenter="0"
            verticalCenter="0"
            skinClass="MyInlineSkin" />

</FxApplication>

This entry is based on a beta version of the Flex Gumbo SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex Gumbo SDK.

{ 4 comments… read them below or add one }

1 Fan March 7, 2009 at 5:06 am

Why don’t you provide the working sample any more? I know, not every player will be able to see it, but maybe you coud do it anyway. like that it is much easier and faster to understand what you are demonstrating and you do not have to put the code in a project first…

Reply

2 Peter deHaan March 7, 2009 at 8:33 am

Fan,

I haven’t been doing the SWF examples lately since the Flex Gumbo Fx namespace is going to be removed, so all of these examples will need to be updated. So, I didn’t want to go to the effort of creating/uploading SWFs for these examples when I’ll probably have to replace them later anyways. Especially considering it is fairly easy to copy/paste in Flex Builder and see what the code does.

When/if I update all these examples after Flex Gumbo is released I’ll add SWFs.

Peter

Reply

3 Jeff March 18, 2009 at 7:08 pm

Peter -

I was trying this from FB4, but i’m seeing run time errors as i mouse over the box. I’ve copied the main app and skin class directly from your first two examples above. Does this seem like its likely to be a version difference of the SDK?

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at mx.graphics::Path/set data() [E:\dev\i10\frameworks\projects\flex4\src\mx\graphics\Path.as:225]
at mx.states::SetProperty/setPropertyValue() [E:\dev\i10\frameworks\projects\framework\src\mx\states\SetProperty.as:341]
at mx.states::SetProperty/remove() [E:\dev\i10\frameworks\projects\framework\src\mx\states\SetProperty.as:311]
at mx.core::UIComponent/removeState() [E:\dev\i10\frameworks\projects\framework\src\mx\core\UIComponent.as:7992]
at mx.core::UIComponent/commitCurrentState() [E:\dev\i10\frameworks\projects\framework\src\mx\core\UIComponent.as:7781]
at mx.core::UIComponent/setCurrentState() [E:\dev\i10\frameworks\projects\framework\src\mx\core\UIComponent.as:7692]
at mx.core::UIComponent/set currentState() [E:\dev\i10\frameworks\projects\framework\src\mx\core\UIComponent.as:4686]
at mx.components.baseClasses::FxComponent/commitProperties() [E:\dev\i10\frameworks\projects\flex4\src\mx\components\baseClasses\FxComponent.as:213]
at mx.components::FxButton/commitProperties() [E:\dev\i10\frameworks\projects\flex4\src\mx\components\FxButton.as:464]
at mx.core::UIComponent/validateProperties() [E:\dev\i10\frameworks\projects\framework\src\mx\core\UIComponent.as:6167]
at mx.managers::LayoutManager/validateProperties() [E:\dev\i10\frameworks\projects\framework\src\mx\managers\LayoutManager.as:539]
at mx.managers::LayoutManager/doPhasedInstantiation() [E:\dev\i10\frameworks\projects\framework\src\mx\managers\LayoutManager.as:689]
at Function/http://adobe.com/AS3/2006/builtin::apply()
at mx.core::UIComponent/callLaterDispatcher2() [E:\dev\i10\frameworks\projects\framework\src\mx\core\UIComponent.as:9142]
at mx.core::UIComponent/callLaterDispatcher() [E:\dev\i10\frameworks\projects\framework\src\mx\core\UIComponent.as:9082]

Reply

4 Peter deHaan March 19, 2009 at 7:23 am

Jeff,

Probably. I just verified with Flex Gumbo SDK build 4.0.0.5289 and didn’t see any RTEs.
Try downloading a newer version of the Gumbo SDK from http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4 and see if you still have the same error.

Peter

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: