Title: RE: Scrolling text areas automatically? (Put windows - thanks)

what I have found in my experience when using tables (may not be the same for other widgets)is that an immediate "scroll to bottom" command after a new insert command did not take me all the way to the bottom(the last row was never visible). To get around this I put the "scroll to bottom" command in the SwingUtilities invokeLater() call to have the event put on the event queue for later processing. I'm not positive why this worked (why look a gift horse in the mouth), but I surmised that it may have given the model time to "settle down" after a row was inserted and the subsequent scroll command when it arrived had a more accurate picture of the true size of the underlying widget it was being used to scroll through, and thus scrolled appropriately. Like I said, not sure if this was the real reason it worked, but it did and I thus stuck it into my bag of tricks.

Chad

-----Original Message-----
From: Paul Brinkley [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, June 19, 2001 11:09 AM
To: [EMAIL PROTECTED]
Subject: Re: Scrolling text areas automatically? (Put windows - thanks)


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