The following example shows how you can add an icon to a Flex Panel container’s title bar by setting the titleIcon property.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/10/18/adding-a-title-icon-to-a-panel-container-in-flex/ -->
<mx:Application name="Panel_titleIcon_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:String id="lorem" source="lorem.txt" />

    <mx:Panel id="panel"
            title="Panel title"
            titleIcon="@Embed('assets/Panel.png')">
        <mx:Text id="txt"
                text="{lorem}"
                width="500"
                textAlign="justify"
                selectable="false" />
    </mx:Panel>

</mx:Application>

View source is enabled in the following example.

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/10/18/adding-a-title-icon-to-a-panel-container-in-flex/ -->
<mx:Application name="Panel_titleIcon_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();">

    <mx:String id="lorem" source="lorem.txt" />

    <mx:Script>
        <![CDATA[
            import mx.controls.Text;
            import mx.containers.Panel;

            [Embed("assets/Panel.png")]
            private const PanelTitleIcon:Class;

            private var panel:Panel;

            private function init():void {
                var txt:Text = new Text();
                txt.text = lorem;
                txt.selectable = false;
                txt.width = 500;
                txt.setStyle("textAlign", "justify");

                panel = new Panel();
                panel.title = "Panel title";
                panel.titleIcon = PanelTitleIcon;
                panel.addChild(txt);
                addChild(panel);
            }
        ]]>
    </mx:Script>

</mx:Application>
 
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.

4 Responses to Adding a title icon to a Panel container in Flex

  1. Dave M says:

    Do you know whether it’s possible to have a rollover state on the titleIcon? I’m struggling with this as it is just a class property, I can’t create a rollover-equipped icon in advance and ‘plug it in’.

    If you have any suggestions of possible solutions, I’d love to hear them :)

  2. Ram Sambamurthy says:

    hi:

    i tried doing the same for an AIR application for the Application’s titleIcon, so that the main window of the application will have a custom icon instead of the standard AIR icon.

    it just won’t work. however, if i use the same gif/png icon for a panel it works fine.

    any suggestions?

    ram

  3. tom says:

    This worked great. Thanks. Just one question – is it possible to add some images (icons) to the bottom bar of Panel using xml syntax? I don’t think it can be done this way.

  4. Michael says:

    How would you do this with a spark panel?

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