21 Replies Latest reply on May 10, 2009 8:41 PM by ShaF10

    Error #1034: Type Coercion failed: cannot convert mx.controls::TextArea@7914c29 to myComponents.DocumentTextArea

    ShaF10 Level 1

      Hi Guys

       

      I am dynamically adding new textareas into my arraycollections. The first textarea inserts perfectly into the array, but when the second textarea is inserted into the array I get the error in the title of this thread. Code below:

       

       

      TypeError: Error #1034: Type Coercion failed: cannot convert mx.controls::TextArea@7914c29 to myComponents.DocumentTextArea.
           at test2/getDocObj()[C:\Documents and Settings\Administrator\My Documents\Flex Builder 3\test2\src\test2.mxml:82]
           at test2/checkLength()[C:\Documents and Settings\Administrator\My Documents\Flex Builder 3\test2\src\test2.mxml:42]

       

       

                  private function checkLength(event:KeyboardEvent):void
                  {
                      var textArea:TextArea = new TextArea();
                      textArea = getDocObj(event.currentTarget.id); // ----------------- Error starts here ----------------- //
                      doc.text = event.currentTarget.id;
                     
                      if(event.keyCode == 8 && textArea.length == 0) {
                          removeChildTextArea(textArea, event.currentTarget.id);
                          documentPage.setFocus();
                      }
                      else if(event.keyCode == 8 && (documentPage.height - documentPage.mx_internal::getTextField().height) <= (documentPage.height - documentPage.textHeight)) {
                          documentPage.setFocus();
                      }
                      else if(textArea.height - textArea.mx_internal::getTextField().height >= textArea.height - textArea.textHeight) {
                          //create new textarea
                          var newtextArea:DocumentTextArea = new DocumentTextArea();
                          documentContainer.addChild(newtextArea.getTextArea());
                          newtextArea.getTextArea().setFocus();
                          newtextArea.getTextArea().addEventListener(KeyboardEvent.KEY_UP, checkLength);
                          documentArray.addItem(textArea);
                        
                          if(documentArray.length == 0) {
                              newtextArea.getTextArea().id = (0).toString();
                          }
                          else {
                              documentPointer += 1;
                              newtextArea.getTextArea().id = documentPointer.toString();
                          }
                      }               
                  }

       

                  private function getDocObj(id:int):TextArea
                  {
                      trace("array size "+documentArray.length+" and id size is "+(id).toString());
                      var findTextArea:DocumentTextArea = DocumentTextArea(documentArray.getItemAt((id)));
                      return findTextArea.getTextArea();
                  }

        • 1. Re: Error #1034: Type Coercion failed: cannot convert mx.controls::TextArea@7914c29 to myComponents.DocumentTextArea
          Michael Borbor Level 4

          You're trying to cast a TextArea to this DocumentTextArea, that's the problem why don't you change this line

           var textArea:TextArea = new TextArea();
          

          To this:

           var textArea:DocumentTextArea= new DocumentTextArea();
          
          • 2. Re: Error #1034: Type Coercion failed: cannot convert mx.controls::TextArea@7914c29 to myComponents.DocumentTextArea
            ShaF10 Level 1

            Thanks, that fixed the issue. I have come across another strange problem though, whenever I create 10 instances of the textarea some of the textareas disappear .. :S any ideas ? dies arraycollections have a limit ?

            • 3. Re: Error #1034: Type Coercion failed: cannot convert mx.controls::TextArea@7914c29 to myComponents.DocumentTextArea
              Michael Borbor Level 4

              Probably a layout issue, maybe you're adding them and they're overlapping I'll say your issue goes in that way.

              1 person found this helpful
              • 4. Re: Error #1034: Type Coercion failed: cannot convert mx.controls::TextArea@7914c29 to myComponents.DocumentTextArea
                ShaF10 Level 1

                Thanks, its was a layout issue. I am having another problem with:

                 

                      textArea.getTextArea().textHeight

                 

                When a new textarea is created it should have a fresh textheight, but that is not the case. The textHeight is inherited from the previous textarea and therefore no data can be added to the new textarea. Any ideas on how to fix this ?

                • 5. Re: Error #1034: Type Coercion failed: cannot convert mx.controls::TextArea@7914c29 to myComponents.DocumentTextArea
                  Michael Borbor Level 4

                  Post the code that handles the creation of new TextAreas. I saw you created a custom class in a previous post, probably that's your issue. And in that class that you created I'll recommend you to use super() right after the constructor.

                   

                  If you're posting code I suggest you that use the >> icon and select Syntax Highlighting - Java in order to make the code more readable.

                  • 6. Re: Error #1034: Type Coercion failed: cannot convert mx.controls::TextArea@7914c29 to myComponents.DocumentTextArea
                    ShaF10 Level 1

                    Thank, I was looking for the syntax highlighter to no avail. Below is the function that deals the with the creation of new textareas. As you can see I check the textheight in the IF statements to determine how much space is left in the textarea before a new textarea is created.

                    private function checkLength(event:KeyboardEvent):void
                    {
                        var textArea:DocumentTextArea = new DocumentTextArea();
                        textArea = getDocObj(event.currentTarget.id);
                        
                        if(event.keyCode == 8 && textArea.getTextArea().length == 0) 
                        {
                            removeChildTextArea(textArea.getTextArea(), event.currentTarget.id);
                            documentPage.setFocus();
                        }
                        else if(event.keyCode == 8 && (textArea.getPageHeight() - documentPage.mx_internal::getTextField().height) < (textArea.getPageHeight() - documentPage.textHeight)) 
                        {
                            documentPage.setFocus();
                        }
                        else if(textArea.getPageHeight() - textArea.getTextArea().mx_internal::getTextField().height > textArea.getPageHeight() - textArea.getTextArea().textHeight) 
                        {
                            //create new textarea
                            var newtextArea:DocumentTextArea = new DocumentTextArea();
                            documentContainer.addChild(newtextArea.getTextArea());
                            newtextArea.getTextArea().setFocus();
                            newtextArea.getTextArea().addEventListener(KeyboardEvent.KEY_UP, checkLength);
                            documentArray.addItem(textArea);
                           
                            if(documentArray.length == 0) {
                                newtextArea.getTextArea().id = (0).toString();
                            } else {
                                documentPointer += 1;
                                newtextArea.getTextArea().id = documentPointer.toString();
                            }
                        }                
                    }

                     

                    The documentTextArea class:

                    package myComponents
                    {
                        import mx.controls.TextArea;
                        
                        public class DocumentTextArea
                        {
                            private var textArea:TextArea;
                            private var pageWidth:int = 300;
                            private var pageHeight:int = 300;
                            
                            public function DocumentTextArea()
                            {
                                super();
                                textArea = new TextArea();
                                textArea.width = pageWidth;
                                textArea.height = pageHeight;
                                textArea.horizontalScrollPolicy="off";
                                textArea.verticalScrollPolicy="off";
                            }
                            
                            public function getTextArea():TextArea
                            {
                                return textArea;
                            }
                            
                            public function getPageWidth():int
                            {
                                return pageWidth;
                            }
                            
                            public function getPageHeight():int
                            {
                                return pageHeight;
                            }
                        }
                    }
                    
                    • 7. Re: Error #1034: Type Coercion failed: cannot convert mx.controls::TextArea@7914c29 to myComponents.DocumentTextArea
                      Michael Borbor Level 4

                      I see that your function gets the height from an event listener isn't 

                      the old text component dispatching that event not the new one?

                       

                      Sincerely,

                       

                      Michael

                       

                      El 10/05/2009, a las 15:05, ShaF10 <forums@adobe.com> escribió:

                       

                      >

                      Thanks, its was a layout issue. I am having another problem with:

                      >

                            textArea.getTextArea().textHeight

                      >

                      When a new textarea is created it should have a fresh textheight, 

                      but that is not the case. The textHeight is inherited from the 

                      previous textarea and therefore no data can be added to the new 

                      textarea. Any ideas on how to fix this ?

                      >

                      • 9. Re: Error #1034: Type Coercion failed: cannot convert mx.controls::TextArea@7914c29 to myComponents.DocumentTextArea
                        Michael Borbor Level 4

                        You could do that process after you create your custom text area

                         

                        Sincerely,

                         

                        Michael

                         

                        El 10/05/2009, a las 16:16, ShaF10 <forums@adobe.com> escribió:

                         

                        >

                        Ok, do you have any suggestions on how I can fix it ?

                        >

                        • 10. Re: Error #1034: Type Coercion failed: cannot convert mx.controls::TextArea@7914c29 to myComponents.DocumentTextArea
                          ShaF10 Level 1

                          I've integrated the textheight into the DocumentTextArea class, so each instance will have its own textHeight. code below:

                           

                          public function getTextHeight():int
                          {
                               return textArea.textHeight;
                          }
                          

                           

                          And the following changes have been made to the checkLength function:

                           

                          private function checkLength(event:KeyboardEvent):void
                          {
                              var textArea:DocumentTextArea = new DocumentTextArea();
                              textArea = getDocObj(event.currentTarget.id);
                                                  
                              if(event.keyCode == 8 && textArea.getTextArea().length == 0) 
                              {
                                  removeChildTextArea(textArea.getTextArea(), event.currentTarget.id);
                                  documentPage.setFocus();
                              }
                              else if(event.keyCode == 8 && (textArea.getPageHeight() - textArea.getTextArea().mx_internal::getTextField().height) < (textArea.getPageHeight() - textArea.getTextHeight())) 
                              {
                                  documentPage.setFocus();
                              }
                              else if((textArea.getPageHeight() - textArea.getTextArea().mx_internal::getTextField().height) > (textArea.getPageHeight() - textArea.getTextHeight()))
                              {
                                  //create new textarea
                                  var newtextArea:DocumentTextArea = new DocumentTextArea();
                                  newtextArea.getTextArea().addEventListener(KeyboardEvent.KEY_UP, checkLength);
                                  documentContainer.addChild(newtextArea.getTextArea());
                                  newtextArea.getTextArea().setFocus();
                                  documentArray.addItem(textArea);
                                 
                                  if(documentArray.length == 0) {
                                      newtextArea.getTextArea().id = (0).toString();
                                  } else {
                                      documentPointer += 1;
                                      newtextArea.getTextArea().id = documentPointer.toString();
                                  }
                              }        
                          }
                          

                           

                          Now, it still doesn't work and the textHeight calculation is still carried from the old textArea to the new one.

                          • 11. Re: Error #1034: Type Coercion failed: cannot convert mx.controls::TextArea@7914c29 to myComponents.DocumentTextArea
                            Michael Borbor Level 4

                            I'm reading this through my phone so I think that you're defining your 

                            class wrongly. I'm not sure about this but  what you need to do is 

                            create your class that extends TextArea and not define a TextArea 

                            inside your class.

                             

                              This has become a bit long thread so please close this one and open 

                            a new one.

                             

                            Sincerely,

                             

                            Michael

                             

                            El 10/05/2009, a las 17:03, ShaF10 <forums@adobe.com> escribió:

                             

                            >

                            I've integrated the textheight into the DocumentTextArea class, so 

                            each instance will have its unique counter. code below:

                            >

                            public function getTextHeight():int

                            {

                                 return textArea.textHeight;

                            }

                            >

                            >

                            And the following changes have been made to the checkLength function:

                            >

                            private function checkLength(event:KeyboardEvent):void

                            {

                                var textArea:DocumentTextArea = new DocumentTextArea();

                                textArea = getDocObj(event.currentTarget.id);

                            >

                                if(event.keyCode == 8 && textArea.getTextArea().length == 0)

                                {

                                    removeChildTextArea(textArea.getTextArea(), 

                            event.currentTarget.id);

                                    documentPage.setFocus();

                                }

                                else if(event.keyCode == 8 && (textArea.getPageHeight() - 

                            documentPage.mx_internal::getTextField().height) < 

                            (textArea.getPageHeight() - textArea.getTextHeight()))

                                {

                                    documentPage.setFocus();

                                }

                                else if((textArea.getPageHeight() - 

                            textArea.getTextArea().mx_internal::getTextField().height) > 

                            (textArea.getPageHeight() - textArea.getTextHeight()))

                                {

                                    //create new textarea

                                    var newtextArea:DocumentTextArea = new DocumentTextArea();

                                      

                            newtextArea.getTextArea().addEventListener(KeyboardEvent.KEY_UP, 

                            checkLength);

                                    documentContainer.addChild(newtextArea.getTextArea());

                                    newtextArea.getTextArea().setFocus();

                                    documentArray.addItem(textArea);

                            >

                                    if(documentArray.length == 0) {

                                        newtextArea.getTextArea().id = (0).toString();

                                    } else {

                                        documentPointer += 1;

                                        newtextArea.getTextArea().id = documentPointer.toString();

                                    }

                                }

                            }

                            >

                            >

                            Now, it still doesn't work and the textHeight will carry the 

                            calculation of the old textArea.

                            >

                            • 12. Re: Error #1034: Type Coercion failed: cannot convert mx.controls::TextArea@7914c29 to myComponents.DocumentTextArea
                              Muzak Level 3
                                  var textArea:DocumentTextArea = new DocumentTextArea();
                                  textArea = getDocObj(event.currentTarget.id);

                               

                              These lines make no sense. Do you want to create a new DocumentTextArea instance or do you want to get a reference to an existing one (assuming that is what getDocobj() returns)?

                               

                              You're now doing:

                              - textArea = create new instance

                              - textArea = get reference to existing instance

                               

                              So you're either looking for:

                               

                                   var textArea:DocumentTextArea = new DocumentTextArea();

                               

                              or

                               

                                   var textArea:DocumentTextArea = getDocObj(etc..);

                               

                              And what is it that getDocObj() does exactly that is different from just using event.currentTarget ?

                              If you want to get a reference to the DocumentTextArea that dispatched the event, event.currentTarget will do.

                               

                                   var textArea:DocumentTextArea = event.currentTarget as DocumentTextArea;

                               

                              I'm a bit unclear of where you're getting the KeyBoardEvent from though. A TextArea doesn't dispatch any KeyBoardEvent.

                              • 13. Re: Error #1034: Type Coercion failed: cannot convert mx.controls::TextArea@7914c29 to myComponents.DocumentTextArea
                                Michael Borbor Level 4

                                A TextArea does dispatch keyboard events.

                                 

                                Sincerely,

                                 

                                Michael

                                 

                                El 10/05/2009, a las 17:46, Muzak <forums@adobe.com> escribió:

                                 

                                >

                                >>     var textArea:DocumentTextArea = new DocumentTextArea();

                                >>     textArea = getDocObj(event.currentTarget.id);

                                >

                                These lines make no sense. Do you want to create a new 

                                DocumentTextArea instance or do you want to get a reference to an 

                                existing one (assuming that is what getDocobj() returns)?

                                >

                                You're now doing:

                                - textArea = create new instance

                                - textArea = get reference to existing instance

                                >

                                So you're either looking for:

                                >

                                     var textArea:DocumentTextArea = new DocumentTextArea();

                                >

                                or

                                >

                                     var textArea:DocumentTextArea = getDocObj(etc..);

                                >

                                And what is it that getDocObj() does exactly that is different from 

                                just using event.currentTarget ?

                                If you want to get a reference to the DocumentTextArea that 

                                dispatched the event, event.currentTarget will do.

                                >

                                     var textArea:DocumentTextArea = event.currentTarget as 

                                DocumentTextArea;

                                >

                                I'm a bit unclear of where you're getting the KeyBoardEvent from 

                                though. A TextArea doesn't dispatch any KeyBoardEvent.

                                >

                                1 person found this helpful
                                • 14. Re: Error #1034: Type Coercion failed: cannot convert mx.controls::TextArea@7914c29 to myComponents.DocumentTextArea
                                  Muzak Level 3

                                  Forget the KeyboarEvent thing

                                  You probably should be listening for the change event.

                                  For instance, no Keyboard event is dispatched when a user pastes text into a TextArea instance. A change event will be dispatched however.

                                   

                                  And I'm wondering why you're using an ArrayCollection to store instance id's when all of that is available through the display list already.

                                   

                                  DisplayObjectContainer.getChildIndex();

                                  DisplayObjectContainer.getItemByName();

                                  DisplayObjectContainer.getChildAt();

                                   

                                  Seems to me you're making things more complicated than need be, but maybe I'm missing something.

                                  • 15. Re: Error #1034: Type Coercion failed: cannot convert mx.controls::TextArea@7914c29 to myComponents.DocumentTextArea
                                    ShaF10 Level 1

                                    Top stuff Muzak. I changed my code to

                                     

                                    var textArea:DocumentTextArea = event.currentTarget as DocumentTextArea;

                                     

                                    And its working perfect now !

                                     

                                    Ive only been coding in AS3 for 12 hours so excuse my poor understanding. I was using the arrayCollection to store all the new objects so that it would be easier for me to retrieve them. I would however be interested in learning how to use the other 3 properties you mentioned.

                                     

                                    As for the change/keyboard evernt, I am listening for certain chars on the keyboard such as the backspace. Does the change event allow me to do that ? Or will I also need to implement another listener for the change event ?

                                    • 16. Re: Error #1034: Type Coercion failed: cannot convert mx.controls::TextArea@7914c29 to myComponents.DocumentTextArea
                                      Muzak Level 3
                                              var newtextArea:DocumentTextArea = new DocumentTextArea();
                                              newtextArea.getTextArea().addEventListener(KeyboardEvent.KEY_UP, checkLength);
                                              documentContainer.addChild(newtextArea.getTextArea());
                                              newtextArea.getTextArea().setFocus();
                                              documentArray.addItem(textArea);

                                       

                                      This part seems wrong. You're adding a textArea to the collection that is already in the collection -- documentArray.addItem(textArea);

                                      And I wonder why you're going through the trouble of having a DocumentTextArea class to then reparent the TextArea inside it.

                                       

                                      var ta:TextArea = new TextArea();
                                      ta.width = 300;
                                      ta.height = 300;
                                      ta.addEventListener(KeyboardEvent.KEY_UP, checkLength);
                                      documentContainer.addChild(ta);
                                      

                                      If you're looking to avoid the few extra lines (creating the TextArea and setting its properties), create a utility class with a static method, something along these lines:

                                       

                                       

                                      package {
                                       
                                          import mx.controls.TextArea;
                                          
                                          public class DocumentUtil {
                                              
                                              public static const DEFAULT_WIDTH:int = 300;
                                              public static const DEFAULT_HEIGHT:int = 300;
                                              
                                               public static function getTextArea():TextArea {
                                                  var ta:TextArea = new TextArea();
                                                  ta.width = DEFAULT_WIDTH;
                                                  ta.height = DEFAULT_HEIGHT;
                                                  ta.horizontalScrollPolicy="off";
                                                  ta.verticalScrollPolicy="off";
                                               
                                                  return ta;
                                              }
                                              
                                          }
                                          
                                      }
                                      

                                       

                                       

                                       

                                      You'd then use it like this:

                                       

                                      var ta:TextArea = documentUtil.getTextArea();
                                      ta.addEventListener(KeyboardEvent.KEY_UP, checkLength);
                                      documentContainer.addChild(ta);
                                      
                                      • 17. Re: Error #1034: Type Coercion failed: cannot convert mx.controls::TextArea@7914c29 to myComponents.DocumentTextArea
                                        ShaF10 Level 1

                                        Thanks, Ive already scrapped the arrayCollections and made several major changes. I will be sure to implement the static class example you listed.

                                         

                                        Can you give me an example implementation of the following:

                                         

                                        DisplayObjectContainer.getChildIndex();
                                        DisplayObjectContainer.getItemByName();
                                        DisplayObjectContainer.getChildAt();
                                        

                                         

                                        I did a search and couldn't find anything useful.

                                        • 18. Re: Error #1034: Type Coercion failed: cannot convert mx.controls::TextArea@7914c29 to myComponents.DocumentTextArea
                                          Muzak Level 3
                                          As for the change/keyboard evernt, I am listening for certain chars on the keyboard such as the backspace. Does the change event allow me to do that ? Or will I also need to implement another listener for the change event ?

                                           

                                          Yeah, I missed that before in your code, sorry.

                                          If you're interested in certain keystrokes, then yes, the KeyboardEvent is the way to go.

                                           

                                          Ive only been coding in AS3 for 12 hours so excuse my poor understanding. I was using the arrayCollection to store all the new objects so that it would be easier for me to retrieve them. I would however be interested in learning how to use the other 3 properties you mentioned.

                                           

                                          Storing references to instances is often a good thing, but if you're adding/removing instances alot, it may prevent them from being garbage collected. So when using a collection of references you'll have to make sure you keep it in synch with what is really out there (on the display list).

                                          Here's some basic info on the DisplayList stuff:

                                          http://livedocs.adobe.com/flex/3/html/help.html?content=05_Display_Programming_09.html

                                           

                                          let's say you want to remove the TextArea that dispatched the KeyboardEvent.KEY_UP event, all you need is:

                                           

                                          private function checkLength(event:KeyboardEvent):void {
                                               var ta:TextArea = event.currentTarget as TextArea;
                                               documentContainer.removeChild(ta);
                                          }
                                          

                                           

                                          Info for other DisplayObjectContainer methods can be found here:

                                          http://livedocs.adobe.com/flex/3/langref/flash/display/DisplayObjectContainer.html

                                           

                                          Should be some examples there as well.

                                          • 19. Re: Error #1034: Type Coercion failed: cannot convert mx.controls::TextArea@7914c29 to myComponents.DocumentTextArea
                                            ShaF10 Level 1

                                            Thanks, some informative links.

                                            Ok, I have another question, how do I retrieve the previous textarea? At the moment I don't have anything by which I can identify them, but Im guessing there may be a method which retrieves the previous or next component.

                                            • 20. Re: Error #1034: Type Coercion failed: cannot convert mx.controls::TextArea@7914c29 to myComponents.DocumentTextArea
                                              Muzak Level 3

                                              Depends on what you mean with "previous".

                                              If you want to get the last TextArea added to documentContainer:

                                               

                                              var taLast:TextArea = documentContainer.getChildAt(documentContainer.numChildren - 1) as TextArea;

                                               

                                              If you want to find the TextArea "before" the one that dispatched the KeyboardEvent, find the index of that TextArea (event.currentTarget) and subtract one from its index.

                                               

                                              var ta:TextArea = event.currentTarget as TextArea;
                                              var taIndex:int = documentContainer.getChildIndex(ta);
                                              var taBefore:TextArea = documentContainer.getChildAt(taIndex - 1) as TextArea;
                                              

                                              Note that you'll have to check if there actually is a "previous" TextArea --> documentContainer.numChildren > 1.

                                              And that is assuming documentContainer only contains TextArea instances. If not, you'll have to loop through its children and count the TextAreas, or you could keep track of the amount of TextAreas in a variable when adding/removing them.

                                              • 21. Re: Error #1034: Type Coercion failed: cannot convert mx.controls::TextArea@7914c29 to myComponents.DocumentTextArea
                                                ShaF10 Level 1

                                                excellent. Ok thats working now. I have yet another question, I have made a zoom function which will scale all textareas in the documentContainer. The problem is when I zoom in the textarea looks alittle rough (the text and the edges), is there any way I smoothen thi out ? I hav seen a similar feature for images, hopefully there is one for components.