File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

JScrollPane - scroll tracking

Protorium Exodious

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.
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) {

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

Hope this helps!
[ 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:
subject: JScrollPane - scroll tracking
It's not a secret anymore!