02
Aug
07

Adding icons to Panel containers

Personally, I’m amazed I can write an entire post around setting a single (and simple) style, but yet here I am. The following example shows how you can place an icon of your choosing in the upper-left corner of a Panel container. As an added bonus, at least I create the Panel in both MXML and ActionScript.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/08/02/adding-icons-to-panel-containers/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="horizontal"
        backgroundColor="white"
        applicationComplete="init();">

    <mx:Script>
        <![CDATA[
            [Embed(source='bulletCheck.png')]
            private static const ico:Class;

            private var panel:Panel;

            private function init():void {
                var image:Image = new Image();
                image.source = "http://www.helpexamples.com/flash/images/image2.jpg";

                panel = new Panel();
                panel.titleIcon = ico;
                panel.title = "Brought to you by ActionScript";
                panel.addChild(image);
                addChild(panel);
            }
        ]]>
    </mx:Script>

    <mx:Panel title="Brought to you by MXML" titleIcon="{ico}" id="pan">
        <mx:Image id="img" source="http://www.helpexamples.com/flash/images/image1.jpg" />
    </mx:Panel>

</mx:Application>

View source is enabled in the following example.


7 Responses to “Adding icons to Panel containers”


  1. 1 adam Jan 12th, 2008 at 10:14 am

    How can I add a toolTip to the titleIcon? Thanks. You’re site has helped me tremendously, keep up the good work….please!

  2. 2 adam Jan 12th, 2008 at 10:31 am

    tooltip for titleicon?

  3. 3 peterd Jan 13th, 2008 at 12:50 am

    Adam,

    Not sure if this is the “best” way, but this seems to work (although it does use mx_internal namespace, so I can’t guarantee it will work this way in the future (or in past versions).

    Since I don’t have a very high confidence level in this code, I’ll just post it in the comments here instead of making it a new entry.

    <?xml version="1.0" encoding="utf-8"?>
    <!-- http://blog.flexexamples.com/2007/08/02/adding-icons-to-panel-containers/ –>
    <mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml”
            layout=”vertical”
            verticalAlign=”middle”
            backgroundColor=”white”>
    
        <mx:Script>
            <![CDATA[
                import mx.core.UIComponent;
                import mx.controls.Image;
    
                private function init():void {
                    var titleBar:UIComponent = panel.mx_internal::getTitleBar();
                    var titleIcon:Bitmap = panel.mx_internal::titleIconObject;
    
                    var icon:Image = new Image();
                    icon.source = new Bitmap(titleIcon.bitmapData);
                    icon.toolTip = “I’m a mystery tool tip”;
                    icon.width = titleIcon.width;
                    icon.height = titleIcon.height;
                    icon.x = titleIcon.x;
                    icon.y = titleIcon.y;
    
                    var depth:int = titleBar.getChildIndex(titleIcon);
                    titleBar.removeChildAt(depth);
                    titleBar.addChildAt(icon, depth);
                }
            ]]>
        </mx:Script>
    
        <mx:Panel id=”panel”
                title=”My icon has a tool tip”
                titleIcon=”@Embed(’asterisk_orange.png’)”
                width=”320″
                height=”120″
                creationComplete=”init();” />
    
    </mx:Application>
    

    You may want to try asking on a high volume list, such as FlexCoders and see if you can get any other suggestions. It may be better to extend the Panel class and use an Image control instead of the Bitmap display object. Plus, by extending Panel, you could add a custom parameter (such as titleIconToolTip which would make the code easier to reuse.

    Hope that helps,
    Peter

  4. 4 adam Jan 13th, 2008 at 4:48 pm

    Peter,

    Thank you for the quick response. Your code worked first time around for me, so thank you for that as well. I will take your advice, I have already posted the question at FlexCoders. I agree best method probably would be extending the Panel. Peter…thanks again.

  5. 5 Stranger Jan 29th, 2008 at 10:11 pm

    Hi,

    I want to shuffle the panels. Say for Example, if i have two panels that are created at run time using action script. I have one panel after the other in a canvas vertically. If i drag one panel and drop it over the other panel, the dragged panel must be placed on the position where the other panel is and the entire order of panels must get shuffled.

    If its confusing, in the above example, if i drag the left panel and drop it over the right panel, the right should come to left and the left panel should come to right.

    How can i do this.

    Any suggestions or example.

    Thanks for the help in advance.

    Stranger

  6. 6 Joel Jun 27th, 2008 at 8:02 am

    Is there any way of getting around embedding the titleIcon and being able to populate it dynamically?

  7. 7 peterd Jun 27th, 2008 at 10:41 am

    Joel,

    Check out Ben Stucki’s IconUtility class at IconUtility Component for Dynamic Run-Time Icons.

    Peter

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".