Home Flex Flex dispatch event example

Related Tutorials


 
 

Share on Google+Share on Google+

Flex dispatch event example

Advertisement
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

 

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 9, 2008

Related Tutorials

Discuss: Flex dispatch event example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com