tags:

views:

43

answers:

3

Consider the following mx:TextArea ..

<mx:TextArea id="textArea"/>

.. periodically being updated with new lines of text using ..

textArea.text += newLineBeingAdded + "\n";

.. how do I make sure the textarea is scrolled down to the bottom so that the last line added is visible to the user?

A: 

I don't know of a way to do this explicitly with the Halo TextArea, but the Spark TextArea has a scrollToRange method that may be of use to you in this context. Another way to approach this if you don't want to use Spark components is to use a Halo List component and add each of your new lines as a new item in the List's data provider. After each addition to the data provider you can call scrollToIndex on the List to ensure the new item is visible. Hope that helps.

Wade Mueller
+1  A: 

after adding text set

callLater(scrollToEnd);

....

function scrollToEnd():void {
   textArea.verticalScrollPosition = int.MAX_VALUE;
}

and it should scroll to the end.

Even better than callLater would be to use invalidateProperties/commitProperties.

Sam
Cool trick, good to know.
Wade Mueller
Sam: I tried that, but unfortunately that only scrolls down to the second last line. It seems like this method might work sometimes, but not always.
knorv
You might try calling validateNow() after adding the new text, but before setting verticalScrollPosition so that the TextArea knows the size of the new text.
joshtynjala
@joshtynjala, you should avoid calling `validateNow()` as it can introduce performance problems. I updated the code to use `callLater` which is better. `invalidateProperties`/`commitProperties` would be even better yet.
Sam
A: 

Put the cursor at the end of the text. That should also scroll to show the cursor:

textArea.selectionBeginIndex =  textArea.text.length;
textArea.selectionEndIndex =  textArea.text.length;
bug-a-lot