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/