27
Nov
08

Auto-scrolling a TextArea control in Flex

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>

7 Responses to “Auto-scrolling a TextArea control in Flex”


  1. 1 Michael Richardson Dec 5th, 2008 at 10:28 pm

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

  2. 2 Peter deHaan Dec 5th, 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

  3. 3 Mandy Jan 20th, 2009 at 11:31 pm

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

  4. 4 Rogelio Lawrence Jan 31st, 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 :)

  5. 5 Peter deHaan Jan 31st, 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

  6. 6 Sam Feb 11th, 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

  7. 7 Ganaraj Apr 21st, 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 ..

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".




Badge Farm

  • Powered by Redoable 1.2
  • Cornify
  • Feeds burnt by Feedburner
  • Feed