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.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

12 Responses to Adding icons to Panel containers

  1. adam says:

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

  2. adam says:

    tooltip for titleicon?

  3. peterd says:

    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. adam says:

    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. Stranger says:

    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. Joel says:

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

  7. peterd says:

    Joel,

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

    Peter

  8. FlexBeginner says:

    Hi,

    Is there a way to make the image thats been added to panel titleBar to make it visible and invisible with a button click ?

    Thank you for the help in advance

  9. Barry Becker says:

    Is it possible to have the icon positioned on the far right instead of on the left?

  10. john desouza says:

    HI All,

    I need to display image in canvas. The image i am displaying is larger then the panel, due to which i cn’t see the image completely. I tried to add the scroll bars, which i am able to display but they are disabled. Please let me know the solution.

    Thanks,
    Jhon

  11. john desouza says:

    Sorry I need to display image in panel but not in canvas.

  12. Asha says:

    Is it possible to have the icon positioned to the far right instead of on the left?

Leave a Reply

Your email address will not be published.

You may 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