Flex dispatch event example


Below mxml file demonstrates the working of dispatchEvent() method. Method works like a driver that drives the event mentioned in its constructor.

Flex dispatchEvent example

     

The example below demonstrates the working of flex dispatchEvent() method. Method works like a driver that drives the event mentioned in its constructor. This method is used to manually dispatch or run away an event. Method, to dispatch any event on the component can be applied either with the component’s id or mouseOver attribute. With mouseOver attribute the method will be used inside the mxml tags. This means with and inside a control tag attribute, mouseOver the method can be dispatched on any component. 

Note: With id attribute, method can be accessed through inside as well as outside the mxml tags.
Note: mxml tags are the tags used inside the flex application root tag. eg. Flex component tags like control and container tags.

Syntax for using the method

component’s id.dispatchEvent(event_ type, boolean value, boolean value);

Example for event_type used in the method constructor is MouseEvent.CLICK.
eg:–   b1.dispatchEvent(MouseEvent.CLICK, false ,true);

Inside the method constructor, the first parameter is the event type specified that will be run or dispatch away by the dispatchEvent method and the second and third parameter are optional parameters that accept boolean values true and false.

Now in the example below, when the mouse arrow is passed over the component in which the dispatchEvent  method is invoked,
method dispatches the MouseEvent.CLICK type event on the component.
With this method, the click based events on components like buttons, check boxes etc can be manually set to run before the MouseEvent.CLICK i.e. mouse click, actually takes place.

 

dispatch.mxml

<?xml version = '1.0' encoding = 'utf-8'?>
<mx:Application xmlns:mx = 'http://www.adobe.com/2006/mxml'
 initialize = 'handler()'>

  <mx:Script>
  
  public function handler():void{
bull.addEventListener(MouseEvent.MOUSE_OVER, dispatcher);
bull.addEventListener(MouseEvent.CLICK, task);
  }

  public function dispatcher(e:Event):void{
  var result:Boolean = bull.dispatchEvent(
  new MouseEvent(MouseEvent.CLICK , false, false));

  }

  public function task(e:Event):void{
  lab1.text += 'Trinity' + '\n';
  txt.text += 'Ne';
  }

  </mx:Script>
  
  <mx:Button id = 'bull' label = 'visit over me'/>
  <mx:Label id = 'lab1'/>
  <mx:TextArea id = 'txt' width = '20%
  height = '5%' editable = 'false'/>

  <mx:Script>
  import mx.controls.Alert;

  public function handler1(event:Event):void{
Alert.show('Event ' + event.type + ' has been dispatched');
  }

  </mx:Script>
  <mx:CheckBox id = 'ch' label = 'move arrow over me'
  click = 'ch.addEventListener(MouseEvent.CLICK, handler1)'
  mouseOver = 'ch.dispatchEvent(
  new MouseEvent(MouseEvent.CLICK, true, false))'
  selected = 'true'/>

</mx:Application>

 

 

dispatch.swf

 

Download the code

 

Copy from source: http://www.roseindia.net/flex/dispatch-event.shtml

Advertisements
By dbglory Posted in Flex

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s