The following example shows how you can insert and append text into a Spark TextArea control in Flex 4 by using the insertText() and appendText() methods.

appendText() — Appends the specified text to the end of the RichEditableText, as if you had clicked at the end and typed it. When RichEditableText supports vertical scrolling, it will scroll to ensure that the last line of the inserted text is visible.
insertText() — Inserts the specified text as if you had typed it. If a range was selected, the new text replaces the selected text; if there was an insertion point, the new text is inserted there, otherwise the text is appended to the text that is there. An insertion point is then set after the new text.

Full code after the jump.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see http://www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.
For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/08/04/inserting-and-appending-text-into-a-spark-textarea-control-in-flex-4/ -->
<s:Application name="Spark_TextArea_insertText_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">
 
    <fx:Script>
        <![CDATA[
            protected var counter:uint = 0;
 
            protected function insertBtn_clickHandler(evt:MouseEvent):void {
                txtArea.insertText(counter + ") " + txtInput.text + "\n");
                counter++;
            }
 
            protected function appendBtn_clickHandler(evt:MouseEvent):void {
                txtArea.appendText(counter + ") " + txtInput.text + "\n");
                counter++;
            }
        ]]>
    </fx:Script>
 
    <mx:ApplicationControlBar width="100%" cornerRadius="0">
        <s:TextInput id="txtInput"
                text="Enter text here" />
        <s:Button id="insertBtn"
                label="insertText()"
                click="insertBtn_clickHandler(event);" />
        <s:Button id="appendBtn"
                label="appendText()"
                click="appendBtn_clickHandler(event);" />
    </mx:ApplicationControlBar>
 
    <s:TextArea id="txtArea"
            horizontalCenter="0"
            verticalCenter="0" />
 
</s:Application>

View source is enabled in the following example.

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

2 Responses to Inserting and appending text into a Spark TextArea control in Flex 4

  1. metali says:

    Hello,

    it’s a good news that spark integrate this function but how can we do with a mx textarea component?
    Because there isnt Spark RichTextEditor, and mx RichTextEditor uses mx textArea…
    So how can we insert text into a mx textArea?

    2nd question: why is there no automatic function when dropping a String into a textArea?

    Thanks for all your usefull articles !

  2. Toni says:

    Hi,
    Have you found a way to REPLACE a peace of text in the Spark TextArea?
    For example – if you have typed “aaa”, then use some trick to replace it with “bbbbbb”.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree