Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4201999

JTextArea's don't automatically scroll when appending() to them.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 5.0
    • 1.2.0, 1.2.2, 1.3.0, 1.4.0, 1.4.1
    • client-libs
    • tiger
    • generic, x86
    • generic, windows_95, windows_nt, windows_2000



      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)
      ======================================================================

            naasunw Naa Naa (Inactive)
            dblairsunw Dave Blair (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: