Auto-scrolling a TextArea control in Flex

by Peter deHaan on November 27, 2008

in TextArea

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>

{ 8 comments… read them below or add one }

1 Michael Richardson December 5, 2008 at 10:28 pm

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

Reply

2 Peter deHaan December 5, 2008 at 10:36 pm

Michael Richardson,

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

Peter

Reply

3 Mandy January 20, 2009 at 11:31 pm

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

Reply

4 Rogelio Lawrence January 31, 2009 at 12:45 pm

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 :)

Reply

5 Peter deHaan January 31, 2009 at 6:30 pm

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

Reply

6 Sam February 11, 2009 at 6:10 am

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

Reply

7 Ganaraj April 21, 2009 at 9:24 am

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 ..

Reply

8 sorin November 2, 2009 at 4:04 am

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

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can 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

Previous post:

Next post: