Home > Flex 4 > Custom focus skin for spark components in Flex 4

Custom focus skin for spark components in Flex 4

In spark components, the focusSkin is treated differently from the normal skin. Unlike the skin of a spark component, the focusSkin is not automatically sized or positioned by the component. Instead, it is added as the first child of the component. In addition, if the focusSkin has a property called target, target is set to point to the parent component.

With this knowledge, it is possible to create custom focusSkins. I’ve included an example of some simple custom focusSkins that I’ve created. Note that each of these focusSkins relies on a few things to make sure that it is updated correctly. They each require a target property that attaches a handler onto the target’s skin in order to update the focusSkin when the skin changes. They also require specific sizing and sometimes positioning inside the overridden updateDisplayList() method.

Now, you may be wondering what’s so great about a couple custom focusSkins? I think the coolest thing about them is the fact that the focusSkins are almost completely drawn using MXML! Take a look at the source for how this was done.

Here are the examples:

View Source

Note: This sample requires Flex SDK 4.0.0.13672 or higher. You can get the latest SDK builds from opensource.adobe.com.

  1. January 29th, 2010 at 08:44 | #1

    This is fantastic, I been building a music application for some time and been thinking a lot about when a user switches focus from/to the player, I wanted a bit of Bang to indicate now the player has a grip on you, will def be trying some of these this weekend.

    Thanks

  2. January 29th, 2010 at 11:11 | #2

    @Tyrone Neill
    Thanks for the feedback! Great to know you will try it. Let me know how it goes!

  3. seth
    February 1st, 2010 at 14:04 | #3

    super helpful post – i had no idea there was a focusskin for textinput. One thing to note, I’m using the 4.0.0.114410 version of the sdk. rather than target, Flex looks for a focusObject. thnx again, Seth

  4. February 3rd, 2010 at 18:44 | #4

    @seth
    Thanks for the comment! If you take a look at SkinnableComponent’s drawFocus() method, you’ll see that focusObject is actually an instance of your focusSkin. Then, “target” is just a property of the focusObject that the component tries to set to itself.

  5. August 19th, 2010 at 10:16 | #5

    Excellent effects the one i like most is the bouncing arrows effect, i will do something like that in an ecommerce app i making for the university.

    Daniel SL

  6. manuel
    November 16th, 2010 at 07:20 | #6

    great example.
    very helpful & effective.

    I get it

    Thks a lot

  7. Steve Berube
    March 31st, 2011 at 20:47 | #7

    Have you found a way to put the focusskin INSIDE the component? I am trying to add an inner border on a focus however it is goes behind the component instead of ontop of it. the depth attribute didn’t seem to help. Any ideas?

  8. Jeps
    April 1st, 2011 at 04:49 | #8

    Nice example.

    I need to add focus for a DateField in my application. How do I do that for DateField. I tried using it by setting the SKinClass to the skin name, but it does not seem to work.

    Any help?

  9. Santanu
    June 20th, 2011 at 13:08 | #9

    Great post. This is fantastic.

  10. October 26th, 2011 at 09:39 | #10

    @Steve Berube
    Sadly, I’m not sure this is possible without hacking around in SkinnableComponent. This is because the focusSkin is added as the bottom child of the SkinnableComponent meaning everything else is laid out on top of it. Of course, if your component’s skin is transparent, you might be able to do something interesting.

  11. October 26th, 2011 at 09:40 | #11

    @Jeps
    This will only work with Spark components. You’ll have to wait for the Spark version of DateField.

  12. Marc Kassay
    April 2nd, 2012 at 10:02 | #12

    @ Jeps
    You can listen for rollOver and rollOut events of a skin and then manipulate an overlay graphic that sits on top of your component. For instance, as you may know there is a ‘background’ Rect in alot of skins that has the lowest depth. Create an exact copy of this Rect and paste it at the end of the Skin file so that its depth is the highest. Give it a id name of ‘topground’ with an alpha value of 0 in the mxml tag. Now listen for those events I mentioned directly in the skin class and manipulate the alpha value. This is of course an example which will hopefully show you that you can do much more visually, such as using filters. I hope that helps.

  13. Marc Kassay
    April 2nd, 2012 at 10:04 | #13

    @Marc Kassay

    And my comment was directed to Steve Berube, not Jeps.

  14. Andrew
    September 20th, 2012 at 13:39 | #14

    I’d like to use these for a Button, but when I do the code “this.setActualSize(target.width, target.height);” throws and error.

  15. mallik
    March 4th, 2013 at 05:57 | #15

    good example really helpfull.

  16. Ven
    April 18th, 2013 at 08:33 | #16

    Very useful.. very handy.. Thanks.

  1. July 31st, 2011 at 01:02 | #1