2

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>
spring
  • 18,009
  • 15
  • 80
  • 160
  • Just a guess: What if you assign a different, distinguishable dataprovider in `initFontSizeData()`? Maybe the method is executed before the value of the child tag is evaluated? – null Apr 15 '16 at 15:08
  • One last idea: does it make a difference if you create the dp in mxml, but in a fx:declarations like in the example code in the link in your question? – null Apr 15 '16 at 17:09

1 Answers1

1

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 :

  • Using mx.core.mx_internal to commit the selected index :
Menu(fontSizePopUp.popUp).selectedIndex = 3;
Menu(fontSizePopUp.popUp).mx_internal::commitSelectedIndex(3);
  • Dispatching an mx.events.MenuEvent event :
var menu_event:MenuEvent = new MenuEvent(MenuEvent.ITEM_CLICK);
    menu_event.index = 3;

Menu(fontSizePopUp.popUp).dispatchEvent(menu_event);
  • Dispatching an mx.events.FlexEvent event :
Menu(fontSizePopUp.popUp).selectedIndex = 3;
Menu(fontSizePopUp.popUp).dispatchEvent(new FlexEvent(FlexEvent.VALUE_COMMIT));

Hope that can help.

akmozo
  • 9,829
  • 3
  • 28
  • 44