2 Replies Latest reply on Sep 11, 2009 2:20 PM by rul987

    Change States using Menu Bar

    rul987

      I am new to flex. The first website I made I used states for navigation. I made states for another small site I am working on.

      This is the problem I have, I need to change states using the MenuBar. I know I should listen for clickevents but I am not sure how to do the

      linking. Most online help and reference material I have use Navigation Containers. However I don't want to change the states.

      Just wondering if someone has some code which uses menu bar and states together.

        • 1. Re: Change States using Menu Bar
          Andrew Rosewarn Level 3

          Hi there

           

          Take a look at this example;

           

          <?xml version="1.0"?>

           

          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="initCollections();" >

              <mx:states>

                  <mx:State name="state2">

                      <mx:RemoveChild target="{label1}"/>

                      <mx:AddChild position="lastChild">

                          <mx:Label text="THIS IS STATE 2"/>

                      </mx:AddChild>

                  </mx:State>

              </mx:states>

           

              <mx:Script>

                  <![CDATA[

           

                      import mx.events.MenuEvent;

                      import mx.controls.Alert;

                      import mx.collections.*;

           

                      [Bindable]

                      public var menuBarCollection:XMLListCollection;

           

                      private var menubarXML:XMLList =

                          <>

                              <menuitem label="Menu1" data="top">

                                  <menuitem label="First State" data=""/>

                                  <menuitem label="Second State" data="state2"/>

                              </menuitem>

           

                          </>;

           

                    private function initCollections():void {

                          menuBarCollection = new XMLListCollection(menubarXML);

                      }

           

                      private function menuHandler(event:MenuEvent):void  {

                          currentState = event.item.@data;

                      }

                   ]]>

              </mx:Script>

           

          <mx:MenuBar labelField="@label" itemClick="menuHandler(event);"

                  dataProvider="{menuBarCollection}" />

               <mx:Label text="THIS IS STATE 1 - DEFAULT" id="label1"/>

          </mx:Application>

           

          I hope this helps, make sure you call your states the same as the data attribute in the XML

           

          Andrew

          1 person found this helpful
          • 2. Re: Change States using Menu Bar
            rul987 Level 1

            Thanks Andrew, I am going to try this out and see if it works.