The following example shows how you can auto-scroll a Flex TextArea control when new content is added by setting the verticalScrollPosition property to the value of the maxVerticalScrollPosition property.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/11/27/auto-scrolling-a-textarea-control-in-flex/ -->
<mx:Application name="TextArea_maxVerticalScrollPosition_text"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();">

    <mx:Script>
        <![CDATA[
            private var timer:Timer;

            private function init():void {
                timer = new Timer(500);
                timer.addEventListener(TimerEvent.TIMER, onTimer);
                timer.start();
            }

            private function onTimer(evt:TimerEvent):void {
                var now:String = new Date().toTimeString();
                var str:String = "[" + timer.currentCount + "] " + now;
                textArea.text += str + "\\n";
                textArea.validateNow();
                textArea.verticalScrollPosition = textArea.maxVerticalScrollPosition;
            }
        ]]>
    </mx:Script>

    <mx:TextArea id="textArea"
            width="200"
            height="160" />

</mx:Application>

View source is enabled in the following example.

Or, instead of calling the validateNow() method before using the verticalScrollPosition and maxVerticalScrollPosition properties, you could use the callLater() method to handle the scrolling, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/11/27/auto-scrolling-a-textarea-control-in-flex/ -->
<mx:Application name="TextArea_maxVerticalScrollPosition_text"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();">

    <mx:Script>
        <![CDATA[
            private var timer:Timer;

            private function init():void {
                timer = new Timer(500);
                timer.addEventListener(TimerEvent.TIMER, onTimer);
                timer.start();
            }

            private function onTimer(evt:TimerEvent):void {
                var now:String = new Date().toTimeString();
                var str:String = "[" + timer.currentCount + "] " + now;
                textArea.text += str + "\\n";
                callLater(autoScrollTextArea, [textArea]);
            }

            private function autoScrollTextArea(target:TextArea):void {
                target.verticalScrollPosition = target.maxVerticalScrollPosition;
            }
        ]]>
    </mx:Script>

    <mx:TextArea id="textArea"
            width="200"
            height="160" />

</mx:Application>
 
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.

10 Responses to Auto-scrolling a TextArea control in Flex

  1. Could you show some examples? I’d like to see what it is that you’re actually producing.

  2. Peter deHaan says:

    Michael Richardson,

    You’re in luck! Copy and paste the code into Flex Builder and you can see what it does. ;)

    Peter

  3. Mandy says:

    Do you have a way to auto scroll data grid? Thanks

  4. Thanks for that Peter – even as a relative beginner, I was able to copy/paste and get it working – and got some brownie points from my boss a a result :)

  5. Peter deHaan says:

    Mandy (not that I expect you’re still awaiting a response),

    I posted a solution for auto-scrolling a Flex DataGrid control at “Creating an auto-scrolling DataGrid control in Flex”.

    Peter

  6. Sam says:

    Thanks for this USEFULL site Peter !!!

    Do you have a way to show the first or last position on a text that is larger than a ?
    When the user/an AS script set a text longer than a InputText, the cursor stay on the last position.
    In the most case, users want to see the begening of the text, but when I set the cursor position, this does not make the InpuText control to show the beginning of the text.

    Thanks

  7. Ganaraj says:

    I found that using Binding is much better than using the timer to continuously check for the scroll position

    something like…

    BindingUtils.bindProperty(inputBox,"verticalScrollPosition",inputBox,"maxVerticalScrollPosition");
    

    yeah .. thats it. Just one line ..

  8. sorin says:

    valueCommit=”id.verticalScrollPosition = id.maxVerticalScrollPosition” tag on the TextArea in mxml works great for me

  9. sachin says:

    hello sir
    My name is sachin n i am trying to make VBox container which ll store all news came from XML File, and that container should scroll automatically and smoothly also (i tried by timer but its not smoothly) and when user will click on any news heading, that specific news should open in another popup, simultaniasly if user only moves cursor on any news scrolling should stop automatically, n on mouseout then it should resume the scrolling .. pls help me

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