Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / March 2006

Tip: Looking for answers? Try searching our database.

Can't get my JTextArea to scroll to the bottom

Thread view: 
Paul Tomblin - 25 Mar 2006 19:31 GMT
I have a FileTailerDocument that extends PlainDocument - it spawns a
thread that reads a file, and when the file gets new text in it, it's
inserted into the Document using
 private void insertText(String textToInsert)
 {
   writeLock();
   try
   {
     int insertLen = textToInsert.length();
     int afterSize = this.getLength() + insertLen;
     if (afterSize > bufferSize)
     {
       this.remove(0, afterSize - bufferSize);
     }
     this.insertString(this.getLength(), textToInsert, null);
   }
   catch (BadLocationException ble)
   {
     ble.printStackTrace();
   }
   writeUnlock();
 }

The part of the program written by the other team uses that Document in
their JTextArea, by doing a
   playbackStatusTextArea.setDocument(logController.getTailerDocument());
   playbackStatusTextArea.setEditable(false);
   doc.addDocumentListener(
       new DocumentListener() {
           public void changedUpdate(DocumentEvent e) {
           }
           public void removeUpdate(DocumentEvent e) {
           }
           public void insertUpdate(DocumentEvent e) {
             // On insert, scroll to the bottom
             SwingUtilities.invokeLater(new Runnable() {
               public void run() {
                 playbackStatusTextArea.setCaretPosition(
                   logController.getTailerDocument().getLength());
               }
             });
           }
       });
But when I update the file, they end up scrolling to a few lines before
the bottom.  I've tried printing out the length in the insertUpdate, and
it is the same as the length of the file in bytes.

I've tried this on Mac and Linux, and it's the same result.  (The
writeLock()/writeUnlock() and SwingUtilities.invokeLater() were
unsuccessful attempts to make it work right.)

Signature

Paul Tomblin <ptomblin@xcski.com> http://xcski.com/blogs/pt/
A good landing is one you walk away from.  A *great* landing is one after
which you can use the plane another time.

Knute Johnson - 26 Mar 2006 08:22 GMT
> I have a FileTailerDocument that extends PlainDocument - it spawns a
> thread that reads a file, and when the file gets new text in it, it's
[quoted text clipped - 46 lines]
> writeLock()/writeUnlock() and SwingUtilities.invokeLater() were
> unsuccessful attempts to make it work right.)

I don't really know what you are up to reading a file in a Document but
you can set your cursor position in the insertString() of Document.
insertString() is most likely being executed in the Event Dispatch
Thread already.  Call super.insertString() to add the string to your
document then call setCursorPostion(getLength()) on the JTextComponent.
 There is no way to find it from the document so you will just have to
make it visible or use a method to set a local value.

Signature

Knute Johnson
email s/nospam/knute/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.