actionscript-3apache-flexflex4flex4.6

Setting the value of a PopUpMenuButton programmatically?


I need to set some initial values in several mx PopUpMenuButton components. Based on this Adobe info I am casting the component as a Menu and setting the selectedIndex – but it isn't working.

Any tips? The function gets called and it appears that the selectedIndex is set but the PopUpMenuButton still displays the first item in its data provider.


       private function initFontSizeData():void {
            Menu(fontSizePopUp.popUp).selectedIndex = 3; // 48 pt

            trace("index", Menu(fontSizePopUp.popUp).selectedIndex);
        }

    <mx:PopUpMenuButton id="fontSizePopUp" 
                        name="fontSizePopUp"
                        width="50" height="20"
                        labelField="label"
                        paddingLeft="-8"
                        itemClick="toolChangeHandler(event)"
                        open="canvasEnabled(false)"
                        close="canvasEnabled(true)"
                        chromeColor="#cccccc"
                        toolTip="Font Size"
                        styleName="toolPopUpStyle"
                        creationComplete="initFontSizeData()"
                        popUpStyleName="toolPopUpStyle">
        <mx:dataProvider>
            <fx:Array>
                <fx:Object fontPointSize = "18" label="18 pt"/>
                <fx:Object fontPointSize = "24" label="24 pt" />
                <fx:Object fontPointSize = "36" label="36 pt" />
                <fx:Object fontPointSize = "48" label="48 pt" />
                <fx:Object fontPointSize = "60" label="60 pt" />
                <fx:Object fontPointSize = "72" label="72 pt"/>
                <fx:Object fontPointSize = "96" label="96 pt" />
            </fx:Array>
        </mx:dataProvider>
    </mx:PopUpMenuButton>

Solution

  • For some reason that I ignore (maybe it's just a bug), the label of the PopUpMenuButton control is not updated and that's why you have to force it to do that using one of these methods :

    Menu(fontSizePopUp.popUp).selectedIndex = 3;
    Menu(fontSizePopUp.popUp).mx_internal::commitSelectedIndex(3);
    
    var menu_event:MenuEvent = new MenuEvent(MenuEvent.ITEM_CLICK);
        menu_event.index = 3;
    
    Menu(fontSizePopUp.popUp).dispatchEvent(menu_event);
    
    Menu(fontSizePopUp.popUp).selectedIndex = 3;
    Menu(fontSizePopUp.popUp).dispatchEvent(new FlexEvent(FlexEvent.VALUE_COMMIT));
    

    Hope that can help.