At 01:37 PM 6/19/2001 -0400, Doug Fields wrote:

>I have a JTextArea in a JScrollPane. It's not editable and used only for a 
>running log (i.e. periodically call textarea.append).
>
>However, if I ever scroll up the log, appends no longer scroll it all the 
>way down again.
>
>I was trying to figure out a call such as:
>
>textarea.append("some text");
>textarea.scrollToBottom();
>// or maybe scrollpane.scrollToBottom();
>
>To make sure it always shows the bottom line when a new line is displayed.
>
>I would appreciate it if you have encountered this in the past, if you 
>could tell me how you did it.

The simplest way to set the scrollbar on a text component is to take
advantage of the fact that JTextComponents track the caret
position by default.  That is, whenever the caret position changes,
the scrollbar (if one exists) is automatically set to keep it
visible.

In fact, I'm unsure why it's not doing that for you right now.  IIRC,
a call to JTextArea.append() call automatically positions the caret
at the end of the text, which in turn would automatically scroll
the JTextArea to the bottom.  Reasons why this wouldn't work:

- You're using a JTextArea subclass with code that maintains the
   caret's current position.

- The JTextArea is in a ScrollPane only indirectly (i.e., a JTextArea
   inside a JPanel inside a JScrollPane).  News of a change to the caret
   wouldn't make it to the grandparent container.

- This is all a figment of my imagination.  Might be that I'm thinking
   of JEditorPanes, and JTextAreas (JTextAreae? heh) don't do this.
   I'm too busy right now to test it.

If it isn't a matter of turning off some code for you, you might
try copying some of the code from JEditorPane.scrollToReference() to
get the behavior you want.  Look in the try block.

If you don't have the JDK1.x source code, I highly recommend you
download it from java.sun.com.  I've learned a few useful
"dirty tricks" from looking at it.



_______________________________________________
Swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/swing

Reply via email to