We help IT Professionals succeed at work.

Error in Simple Flex Example

armanbena
armanbena asked
on
Hi,

I am just starting out with Flex.  I was following an example and am getting an error in the function where I set the conditions for the label.text. Any ideas???
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
      initialize="init()">

<mx:Script>
      <![CDATA[
      private function init():void{
      button3.addEventListener(MouseEvent.CLICK, handleClick);
      }
      private function handleClick(event:MouseEvent):void {
      if (event.target == button2) {
      -->label.text = 'Button 2 clicked\n';
      }
      else if (event.target == button3) {
      -->label.text = 'Button 3 clicked\n';
      }
      }
      ]]>
</mx:Script>



      <mx:VBox x="57" y="85" height="100%">
            <mx:Button id="button1" label="button1" click="label.text +='Button 1\n'"/>
            <mx:Button id="button2" label="button2" click="label.text +='Button 2\n'"/>
            <mx:Button id="button3" label="button3" click="label.text +='Button 3\n'"/>
            <mx:Label id="label" text="Label"/>
      
      </mx:VBox>
      
</mx:Application>
Comment
Watch Question

Commented:
You will probably find that you are working with the label control in your init method before the label control has been created.

So change the call to init from being at initialize to being creationComplete="init()" rather than  initialize="init()"
Tell me if changing  <mx:Label id="label" text="Label"/>  to  <mx:Label id="label1" text="Label"/> works;
Your code with that modification will appear like below
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
				initialize="init()">
	
	<mx:Script>
		<![CDATA[
				private function init():void{
					button3.addEventListener(MouseEvent.CLICK, handleClick);
				}
				private function handleClick(event:MouseEvent):void {
					if (event.target == button2) {
						label1.text = 'Button 2 clicked\n';
					}
				else 
					if (event.target == button3) {
						label1.text = 'Button 3 clicked\n';
					}
				}
		]]>
	</mx:Script>
	
	
	
	<mx:VBox x="57" y="85" height="100%">
		<mx:Button id="button1" label="button1" click="label1.text +='Button 1\n'"/>
		<mx:Button id="button2" label="button2" click="label1.text +='Button 2\n'"/>
		<mx:Button id="button3" label="button3" click="label1.text +='Button 3\n'"/>
		<mx:Label id="label1" text="Label"/>
		
	</mx:VBox>
	
	
</mx:Application>

Open in new window


Let me know if it work, if yes evitate to use components names as ids.

Author

Commented:
I tried both, and when I changed the label to label1 it worked!!

Thanks

Explore More ContentExplore courses, solutions, and other research materials related to this topic.