Adobe air - add context menu on the right side of node tree

I am using Adobe Air to create a desktop application. In this application, I have a tree and I want to add a menu to a node when it is right clicked. I followed how they say for flexible tree but doesn't work.

Any How-to?

~ Umesh

+2


source to share


1 answer


I'm not really sure about your problem, since you don't say if it didn't compile, display or give a runtime error, etc., so I put together an mxml to demonstrate it. The code is mostly cobbled together from the Tree and Menu examples. See the documentation for more details.



<?xml version="1.0" encoding="utf-8"?><mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.controls.Menu;
        [Bindable]
        public var selectedNode:XML;

        // Event handler for the Tree control change event.
        public function treeChanged(event:Event):void {
            selectedNode=mx.controls.Tree(event.target).selectedItem as XML;
        }

        private var prevMenu:Menu = null

        public function showMenu(event:MouseEvent):void
        {
            if(prevMenu != null)
                prevMenu.hide()
            var menu:Menu = Menu.createMenu(null, menuData, false);
            menu.labelField="@label"
            menu.show(event.stageX, event.stageY)
            prevMenu = menu
        }

    ]]>
</mx:Script>
<mx:XML id="menuData">
    <root>
        <menuitem label="copy" eventName="copy"/>
        <menuitem label="paste" eventName="paste"/>
    </root>
</mx:XML>
<mx:XMLList id="treeData">
    <node label="Mail Box">
        <node label="Inbox">
            <node label="Marketing"/>
            <node label="Product Management"/>
            <node label="Personal"/>
        </node>
        <node label="Outbox">
            <node label="Professional"/>
            <node label="Personal"/>
        </node>
        <node label="Spam"/>
        <node label="Sent"/>
    </node>    
</mx:XMLList>
<mx:Panel title="Tree Control Example" height="75%" width="75%" 
    paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10">

    <mx:Label width="100%" color="blue" 
        text="Select a node in the Tree control."/>

    <mx:HDividedBox width="100%" height="100%">
        <mx:Tree id="myTree" width="50%" height="100%" labelField="@label"
            showRoot="false" dataProvider="{treeData}" change="treeChanged(event)"
            rightClick="showMenu(event)"/>
        <mx:TextArea height="100%" width="50%"
            text="Selected Item: {selectedNode.@label}"/>
    </mx:HDividedBox>

</mx:Panel></mx:WindowedApplication>

      

+4


source







All Articles