aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JScrollPane - scroll tracking Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JScrollPane - scroll tracking" Watch "JScrollPane - scroll tracking" New topic
Author

JScrollPane - scroll tracking

Protorium Exodious
Greenhorn

Joined: Dec 10, 2001
Posts: 6
Hiya, Im using a JTextArea and adding a JScrollPane to it using new JScrollPane(clientConsole) where clientConsole is the JTextArea
When the textarea fills up, I have to manually scroll down to see new text being added, how can I make it automatically scroll down
Im using it for my chat program - first go at Java so still inexperienced - its the last thing I need to do though
thx for any help
Jeff Wisard
Ranch Hand

Joined: Jan 07, 2002
Posts: 89
Try this:
Whenever you add text to your text area (i.e. when you call the setText() method), set the caret position of the JTextArea also.
clientConsole.setCaretPosition(clientConsole.getText().length());
This will move make the JScrollPane scroll to the bottom of the JTextArea.
An easy way to do this is to add a DocumentListener to your JTextArea. When the text of the JTextArea changes, the listener is called. Set the caret position in the listener and then you never need to worry about it again.

clientConsole.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
clientConsole.setCaretPosition(clientConsole.getText().length());
}

public void insertUpdate(DocumentEvent e){}
public void removeUpdate(DocumentEvent e){}
});

Hope this helps!
-Jeff
[ January 14, 2002: Message edited by: Jeff Wisard ]

Jeff Wisard<br />Sun Certified Java Programmer (Java 2)<br />Sun Certified Web Component Developer
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JScrollPane - scroll tracking