Adobe air - add context menu on right click of tre

2019-05-26 19:58发布

问题:

I am using Adobe Air to create a desktop application. In that application I have a tree and i want to add menu to a node when it right clicked. I followed the way they are saying for flex tree, but not working.

Any How-to ?

~Umesh

回答1:

I am not exactly sure what your problem is, since you don't say whether it did not compile, did not have display or gave a runtime error etc, so I put together a mxml to demonstrate it. The code is mostly cobbled together from the Tree and Menu examples. Please refer to 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>


标签: air