The following example shows how you can use the itemDoubleClick event to open the selected Tree node in Flex.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/11/29/using-the-itemdoubleclick-event-to-open-nodes-in-a-flex-tree-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="horizontal"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();">

    <mx:Script>
        <![CDATA[
            import mx.events.ListEvent;

            private function tree_itemDoubleClick(evt:ListEvent):void {
                var node:XML = tree.selectedItem as XML;
                var isOpen:Boolean = tree.isItemOpen(node);
                tree.expandItem(node, !isOpen);
            }
        ]]>
    </mx:Script>

    <mx:XMLListCollection id="xmlListColl">
        <mx:source>
            <mx:XMLList>
                <node label="One">
                    <node label="One.1">
                        <node label="One.1.1">
                            <node label="One.1.1.1">
                                <node label="One.1.1.1.1" />
                            </node>
                        </node>
                    </node>
                    <node label="One.2" />
                    <node label="One.3" />
                </node>
                <node label="Two">
                    <node label="Two.1" />
                    <node label="Two.2" />
                    <node label="Two.3" />
                </node>
                <node label="Three" />
            </mx:XMLList>
        </mx:source>
    </mx:XMLListCollection>

    <mx:ApplicationControlBar dock="true">
        <mx:CheckBox id="checkBox"
                label="doubleClickEnabled:"
                labelPlacement="left"
                selected="true" />
    </mx:ApplicationControlBar>

    <mx:Tree id="tree"
            dataProvider="{xmlListColl}"
            labelField="@label"
            width="250"
            rowCount="6"
            doubleClickEnabled="{checkBox.selected}"
            itemDoubleClick="tree_itemDoubleClick(event);"
            showScrollTips="true" />

</mx:Application>

View source is enabled in the following example.

 
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.

3 Responses to Using the itemDoubleClick event to open nodes in a Flex Tree control

  1. razor7 says:

    Hello, i have this ree component in my app, but if I double click on ot, i loose the easi9ng animation function… Any suggestion?

    AS3 function

    private function treeItemClick(event:ListEvent):void {
    	var node:XML = arbolCategorias.selectedItem as XML;
    	var isOpen:Boolean = arbolCategorias.isItemOpen(node);
    	arbolCategorias.expandItem(node, !isOpen);
    }
    

    Thanks in advise

  2. peterd says:

    razor7,

    Try passing a third parameter to the expandItem() method and setting it to true, like this:

    arbolCategorias.expandItem(node, !isOpen, true);
    

    For more information on the expandItem() method, see the Tree.expandItem() documentation for Flex 3 beta.

    Peter

  3. Moyeha says:

    itemDoubleClick event doesn’t work in firefox 3,6 Mac 10,5,8

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