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

JTextArea constructor misplaces caret

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 6
    • client-libs

      FULL PRODUCT VERSION :
      java version "1.6.0-beta2"
      Java(TM) SE Runtime Environment (build 1.6.0-beta2-b86)
      Java HotSpot(TM) Client VM (build 1.6.0-beta2-b86, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Windows 2000 [Version 5.00.2195]

      A DESCRIPTION OF THE PROBLEM :
      According to the documentation

      JTextArea jTextArea = new JTextArea("test");

      should be equivalent to

      JTextArea jTextArea = new JTextArea();
      jTextArea.append("test");

      This is not the case. The constructor with the String parameter resets the caret's position to 0 while the constructor+append combination places the caret at the end of the text.

      If the contents of the JTextArea are displayed in a JScrollPane, using these different constructors yields different results for the behaviour of the viewport.

      The fix is obvious. In JTextArea.java, line 192

      select(0, 0);

      should be replaced by

      int caretPos = text.length();
      select(caretPos, caretPos);

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Please run the attached source code and check the output to System.out and the contents of the viewport.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Using the constructor+append combination results in the desired behaviour. The caret's position is 7 and the viewport shows the last lines.
      ACTUAL -
      Using the constructor with the String parameter sets the caret's position to 0. The viewport shows the first lines.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      /*
       * JTextAreaBugdemo.java
       */

      import javax.swing.JFrame;
      import javax.swing.JScrollPane;
      import javax.swing.JTextArea;
      import javax.swing.SwingUtilities;

      public class JTextAreaBugdemo extends JFrame {
          
          static private JTextArea jTextArea;
          
          public JTextAreaBugdemo() {
              JFrame.setDefaultLookAndFeelDecorated(true);
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              
              jTextArea = new JTextArea("line 0\n");
      // jTextArea = new JTextArea();
      // jTextArea.append("line 0\n");
              
              System.out.println("caret position = " + jTextArea.getCaretPosition());
              
              getContentPane().add(new JScrollPane(jTextArea));
              pack();
              setVisible(true);
          }
          
          public static void main(String[] args) {
              javax.swing.SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                      new JTextAreaBugdemo();
                      jTextArea.append("line 1\n");
                      jTextArea.append("line 2\n");
                      jTextArea.append("line 3\n");
                      jTextArea.append("line 4\n");
                      jTextArea.append("line 5");
                  }
              });
          }
      }
      ---------- END SOURCE ----------

            peterz Peter Zhelezniakov
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: