0

Maybe I am trying to use the wrong component for what I want to do. I have a PopUpMenuButton with 2 items in it. I only want to take action when a user selects an item in the popup menu.

I have set the itemClick event to perform the necessary actions.

The button only displays the menu when the down arrow is clicked, but the itemClick event fires regardless of where the button is clicked. So I am getting the unwanted behavior of action being taken based on the last item selected without a menu being displayed/selected by the user.

How can I distinguish if the event occurred because the user clicked the menu down arrow or the main part of the button?

Should I be approaching this differently?

Thanks,

John

ketan
  • 19,129
  • 42
  • 60
  • 98
user278859
  • 10,379
  • 12
  • 51
  • 74

2 Answers2

0

If you look at this example found here, you can see that the label property of the MenuEvent is accessible. As long as the items in the PopUpMenu have different labels, you can use some simple logic to determine which button was clicked like in the example.

Black Dynamite
  • 4,067
  • 5
  • 40
  • 75
  • I am using the label property and can tell which item is clicked. What I can't figure out is how to tell if the user actually selected an item in the menu. If they click the main part of the button the menu does not pop, but the itemClick event still fires. I want to ignore this click. I do not see how the label property helps in any way especially in my case as I do not let the label change. It always displays "Default Exports..." while the menu displays the available export types. When a type is selected that export type is set up for the user, and the label is reset to "Default Exports...". – user278859 Jan 01 '14 at 02:28
0

How about listening to the popping up menu instance instead of the button?

Steps:

  1. Adding open/close event handlers on the button.
  2. In open/close event handler, we can add ITEM_CLICK event on the popping instance which is the menu exactly.

Code:

<fx:Script>
    <![CDATA[
        import mx.events.DropdownEvent;
        import mx.events.MenuEvent;

        protected function pmb_openHandler(event:DropdownEvent):void
        {
            pmb.popUp.addEventListener(MenuEvent.ITEM_CLICK,onMenuItemClick);
        }

        protected function pmb_closeHandler(event:DropdownEvent):void
        {
            pmb.popUp.removeEventListener(MenuEvent.ITEM_CLICK,onMenuItemClick);
        }

        private function onMenuItemClick(event:MenuEvent):void
        {
            trace(">>>onMenuItemClick on Menu : ", event.item.@label);  
        }

    ]]>
</fx:Script>
<fx:Declarations>
    <!-- A an data provider in E4X format. -->
    <fx:XMLList id="treeDP2">
        <node label="Inbox"/>
        <node label="Calendar"/>
        <node label="Deleted Items"/>
    </fx:XMLList>
</fx:Declarations>

<mx:HBox>
    <mx:PopUpMenuButton 
        id="pmb"
        dataProvider="{treeDP2}"
        labelField="@label"
        open="pmb_openHandler(event)"
        close="pmb_closeHandler(event)"
        />
</mx:HBox>
Robert.S
  • 11
  • 1