Trouble with code to start auto-scrolling log text, stop auto-scroll, resume auto-scroll

Trouble with code to start auto-scrolling log text, stop auto-scroll, resume auto-scroll

Post by opalp » Fri, 17 Mar 2006 05:10:05

he code below creates a JFrame and displays it. Within JFrame is a
window which scrolls text. If user does not interact with program the
most recently appended text is always visible, one way to look at this
behaviour is that veritcal scroll bar is attached to bottom of vertical
scroll pane. If user interacts with program, say by clicking a line,
the scroll bar gets unattached and the line user interacted with
remains in display. Those behaviours satisfy; the missing behaviour:
how does user reattach the vertical scroll bar to the bottom of
vertical scroll pane?

I've played with implementing ChangeListener including looking at model
in ChangeEvent.getSource(), searched the internet (found posts but
solutions did not work for me), and am thinking of going through open
source loggers and chat clients to see how to get the behaviour I
seek. Perhaps someone here will enlighten the way.

Here is a compiling source:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer.*;
import javax.swing.event.*;
/** Automatically scrolling text in panel */
public class TextView extends JPanel {
protected JTextArea textArea;
private JScrollPane scrollPane;
private TextView() {
setLayout(new GridBagLayout());
textArea = new JTextArea(5, 20);
scrollPane = new JScrollPane(textArea,
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridheight = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add(scrollPane, c);
public void addText(final String text) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textArea.append(text + "\n");
private static class HasTextView {
TextView tv = null;
public static TextView makeTextView() {
if (SwingUtilities.isEventDispatchThread())
return new TextView();

final HasTextView envelope = new HasTextView();
try {
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
public void run() { = new TextView();
} catch (Exception e) { //InterruptedException,
throw new RuntimeException("could not make TextView", e);
TextView made =;
if (made == null)
throw new IllegalStateException("expected TextView made
return made;
/**** code to try out the above ****/
private static void startGui(TextView tv) {
JFrame f = new JFrame();
f.setLayout(new BorderLayout());
f.setSize(new Dimension(800, 600));
f.setPreferredSize(new Dimension(800, 600));
public static void main(String args[]) throws InterruptedException
final TextView tv = makeTextView();

Trouble with code to start auto-scrolling log text, stop auto-scroll, resume auto-scroll

Post by Vova Rezni » Fri, 17 Mar 2006 06:02:34

believe you now why it happens:
The text component gets focus and sets caret position
in place where you clicked and didn't change it again.

To be simple just reset caret position each time you append text

textArea.append(text + "\n");


or do the same once on some action, for example focusLost.

I added JButton to a frame and change position on click - works ok.

XXXX@XXXXX.COM opalinski from opalpaweb wrote: