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

{ 9 comments… read them below or add one }
Could you show some examples? I’d like to see what it is that you’re actually producing.
Michael Richardson,
You’re in luck! Copy and paste the code into Flex Builder and you can see what it does. ;)
Peter
Do you have a way to auto scroll data grid? Thanks
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 :)
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
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
I found that using Binding is much better than using the timer to continuously check for the scroll position
something like…
yeah .. thats it. Just one line ..
valueCommit=”id.verticalScrollPosition = id.maxVerticalScrollPosition” tag on the TextArea in mxml works great for me
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