Adobe FlexIn an Adobe AIR app I am working on, I need to programmatically disable / enable a items in a menuBar component, based on Internet connection status. I already have a function that listens for changes in the online status and updates a global variable when it changes, so I know when I loose an Internet connection. Now, I just need to change the disable / enable properties in my menuBar. After several tries, I figured out what I needed to do.

First here is my setup:

In my main MXML file, I have the actual Flex MenuBar component with properties. That line of code looks lke:
<mx:MenuBar x=”0″ y=”0″ id=”cbMainMenu” labelField=”@label” showRoot=”false” width=”100%” buttonMode=”true” dataProvider=”/assets/menu.xml” />

The dataProvider is an XML file that looks like:

________________________________________
<?xml version=”1.0″ encoding=”UTF-8″?>

<mainmenu>
<menuitem label=”File”>
<submenuitem label=”Quit” enabled=”true” />
</menuitem>

<menuitem label=”Help”>
<submenuitem label=”Online Help” enabled=”true” />
<submenuitem label=”Contact Support” enabled=”true” />
</menuitem>
</mainmenu>
________________________________________

In my project, anytime I detect a change in Internet status, I want to set the “Contact Support” option to disabled/enabled.

So to get access to that here is the dot syntax:
cbMainMenu.dataprovider.source[0].menuitem[1].submenuitem[1].@enabled = false;

To break that down it’s:
+ cbMainMenu is my MenuBar component’s ID (change this to match yours)
+ dataProvider is the XML file I have pasted above
+ source[0] is a reference to the actual data in the dataProvider
+ now I just walk the XML tree, skipping the root node, with menuitem[1].submenuitem[1]. That gets the 2nd sub-menu item of the 2nd menu item (remember the count starts at zero)
+ Last is “@enabled” which is the enabled attribute (@). It could have just as easily been @label to change the text.

« »