JDK-4201999 : JTextArea's don't automatically scroll when appending() to them.
  • Type: Bug
  • Status: Resolved
  • Resolution: Fixed
  • Component: client-libs
  • Sub-Component: javax.swing
  • Priority: P4
  • Affected Version: 1.2.0,1.2.2,1.3.0,1.4.0,1.4.1
  • OS: generic,windows_95,windows_nt,windows_2000
  • CPU: generic,x86
  • Submit Date: 1999-01-11
  • Updated Date: 2017-05-16
  • Resolved Date: 2003-09-26
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availabitlity Release.

To download the current JDK release, click here.
Other
5.0 tigerResolved
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Relates :  
Description

Name: dbT83986			Date: 01/10/99


I have tried several ways to get the JTextArea to scroll when 
appending text to it from somewhere OUTSIDE of an event handler,
but none seem to work.

This piece of code in an applet works correctly:


JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta);

public void init() 
{
  getContentPane().add(sp, BorderLayout.CENTER);
}

public void ActionPerformed(ActionEvent e)
{
  ta.append("Test");
}


If I move the ta.append() line outside of the ActionPerformed()
box, however, when the entire JTextArea fills up, it won't
continue scrolling down like I want it to (and like the old
TextAreas do).  The code that doesn't work is as follows (and
this is just a test, mind you):

JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta);

public void init() 
{
  getContentPane().add(sp, BorderLayout.CENTER);

// try { Thread.sleep(1000); } catch (InterruptedException e) {}

  ta.append("Test");
}
(Review ID: 52291)
======================================================================

Name: stC104175			Date: 03/27/2000


bash-2.02$ //e/jdk1.3/bin/java -version
java version "1.3.0rc1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0rc1-T)
Java HotSpot(TM) Client VM (build 1.3.0rc1-S, mixed mode)

When you append() a line to a JTextPane in a JScrollPane, the way of scrolling
depends on how you append it:
a) in a Thread - no scrolling to the new text
b) in a Listener - scrolling to the new text (desirable)
This is inconsistent and IMHO confusing to the user, who doesn't care about
threads/events and just wonders "why is this scrolling differently from that?"

Code to demonstrate:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TestScrolls extends JFrame implements Runnable{

    JScrollPane scroll1;
    JScrollPane scroll2;
    JTextArea text1;
    JTextArea text2;

    public TestScrolls(){
	setSize(300, 300);
	getContentPane().setLayout(new GridLayout(2, 1));
	text1 = new JTextArea();
	text1.setEditable(false);
	text2 = new JTextArea();
	text2.setEditable(false);
	scroll1 = new JScrollPane(text1);
	scroll2 = new JScrollPane(text2);
	getContentPane().add(scroll1);
	getContentPane().add(scroll2);
	MM mm = new MM();
	this.addMouseMotionListener(mm);
	text1.addMouseMotionListener(mm);
	text2.addMouseMotionListener(mm);
	new Thread(this).start();
    }

    public void run(){
	while(true){
	    try{
		Thread.sleep(10);
		text1.append("another 10 milliseconds have passed!\n");
	    }
	    catch(Throwable t){
	    }
	}
    }

    class MM extends MouseMotionAdapter{
	public void mouseMoved(MouseEvent e){
	    text2.append("mouse moved!\n");
	}
    }

    public static void main(String[] args){
	new TestScrolls().setVisible(true);
    }
}
(Review ID: 102906)
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger FIXED IN: tiger INTEGRATED IN: tiger tiger-b22
2004-06-14

EVALUATION The submitter says that TextAreas work correctly, but JTextAreas do not, so I am reassigning this to Swing. eric.hawkes@eng 1999-01-11 -------- In fact this is a bug not a rfe. Appending text in the event dispatching thread scrolls to the appended text appending text not in the event dispatching thread does not. --- TestScrolls.java -- import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TestScrolls extends JFrame implements Runnable{ JScrollPane scroll1; JScrollPane scroll2; JTextArea text1; JTextArea text2; public TestScrolls(){ setSize(300, 300); getContentPane().setLayout(new GridLayout(2, 1)); text1 = new JTextArea(); text1.setEditable(false); text2 = new JTextArea(); text2.setEditable(false); scroll1 = new JScrollPane(text1); scroll2 = new JScrollPane(text2); getContentPane().add(scroll1); getContentPane().add(scroll2); MM mm = new MM(); this.addMouseMotionListener(mm); text1.addMouseMotionListener(mm); text2.addMouseMotionListener(mm); new Thread(this).start(); } public void run(){ Runnable doAppend = new Runnable() { public void run() { text1.append("another 1 second has passed!\n"); } }; while(true){ try{ Thread.sleep(1000); SwingUtilities.invokeLater(doAppend); text2.append("another 1 second has passed!\n"); } catch(Throwable t){ } } } class MM extends MouseMotionAdapter{ public void mouseMoved(MouseEvent e){ text2.append("mouse moved!\n"); } } public static void main(String[] args){ new TestScrolls().setVisible(true); } } --- Converting this rfe to a bug. ###@###.### 2001-11-13 Name: anR10225 Date: 08/29/2003 The default behavior should remain the same because some applications use the fact that caret doesn't update its position when the document mutations are performed not on the Event Dispatching Thread. The way out is to add 'updatePolicy' property to the DefaultCaret which will control the caret updates in response to the document updates. This property allows to disable and enable caret updates independently of the thread where document mutations are performed. ======================================================================
2004-06-11

WORK AROUND Appending text in the event dispatching thread does scrolling. Runnable doAppend = new Runnable() { public void run() { text1.append("another 1 second has passed!\n"); } }; SwingUtilities.invokeLater(doAppend); ###@###.### 2001-11-13
2001-11-13