Search
Feeds
Jun 20 2009

ImageCheckbox Component

Today I had need of a checkbox that implemented an image instead of a standard textual label. For this particular case I needed to use embeded images, so this first version accepts a type of Class for the iconLabel. I simply extended Checkbox and added an image component to the view, careful to update the properties and dimensions.

Feel free to use this code in your apps. Simply provide the labelIcon and image and you should be good to go!

 

package
{
    import mx.controls.CheckBox;
    import mx.controls.Image;

    public class ImageCheckbox extends CheckBox
    {
        private var image:Image;
        private var _labelIcon:Class;
       
        public function ImageCheckbox()
        {
            super();
        }
       
        override protected function createChildren():void
        {
            super.createChildren();
           
            image = new Image();
            this.addChild(image);
        }
       
        override protected function commitProperties():void
        {
            super.commitProperties();
            image.source = labelIcon;
        }
       
        override protected function measure():void
        {
            super.measure();
            image.height = image.content.height;
            image.width = image.content.width;
            this.height = image.height;
            image.x = 16;
            this.width = image.width + 16;
        }
       
        override public function set label(value:String):void
        {
           
        }
       
        public function set labelIcon(licon:Class):void
        {
            _labelIcon = licon;
            this.invalidateProperties();
        }
       
        public function get labelIcon():Class
        {
            return _labelIcon;
        }
    }
}

0 comments - Posted by TJ Downes at 10:43 PM - Categories:

Jun 13 2009

Horizontal or Vertical GridItem for Flex Grids

In the process of reviewing some code prior to publishing, I noticed a lot of VBox components used in the MXML within GridItems. This was used to force the GridItem to lay it's children out vertically, as the GridItem component allows only horizontal layouts. Given that there a considerable number of GridItems in the component I was reviewing, I wanted a way to not just clean up the code, but to also improve overall performance.

A quick Google search showed that many were using the mx_internal namespace methods to override the layout of the HBox. This is all fine and good, but in the long term means that the code is less reusable, and also only allows you to set the direction of the container flow for each individual GridItem. I wanted a more elegant solution. Considering the entire Grid system is based upon HBoxes I didn't guess this would be too hard.

I started by creating my own new class, named CustomGridItem. This extended Box instead of HBox. I copied in all the methods from the GridItem components. I figured this would be enough. However, I had not considered that the Grid and GridRow classes were dependent upon the GridItem class.

In the end I ended up creating all the custom classes to ensure that the proper types were referenced in each class. The end result is what I feel to be a more elegant and portable solution, and also easily allows me to extend the grid and it's related classes in the future.

Thanks to Tom Decaux's "kind" advice, I reviewed the GridItem component and saw that there really was no need to recreate all three classes. My understanding of the mx_internal namespace was in error, and now I see that the direction functionality can be added back into the class easily. I have updated the code examples.

Now I can simply do the following:

<ui:CustomGrid width="100%">

    <ui:CustomGridRow width="100%">

        <ui:CustomGridItem direction="horizontal" width="50%">

            <mx:Label text="my name: " />

            <mx:Label text="TJ" />

        </ui:CustomGridItem>

        <ui:CustomGridItem direction="vertical" width="50%" horizontalAlign="center" verticalAlign="middle">

            <mx:Image source="/path/to/my/picture.jpg"/>

            <mx:Label text="this is me!" />

        </ui:CustomGridItem>

    </ui:CustomGridRow>

</ui:CustomGrid>

The example above is very rudimentary, but this custom griditem class can easily be used to nicely layout complex forms. Feel free to use this code anywhere you want, free of charge!

You can download the component here

0 comments - Posted by TJ Downes at 6:36 AM - Categories: Technical Articles | Adobe AIR | Adobe Flex

Aug 29 2008

Previous Posts

Aug 27 2008
Aug 14 2008
Aug 13 2008
Aug 10 2008