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

TextArea.replaceRange() works incorrectly if text contains line separators

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 6
    • 1.4.2
    • client-libs
    • b08
    • x86
    • windows_2000, windows_xp
    • Verified

      Name: jl125535 Date: 04/01/2004


      FULL PRODUCT VERSION :
      java version "1.5.0-beta2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b43)
      Java HotSpot(TM) Client VM (build 1.5.0-beta2-b43, mixed mode)

      java version "1.4.2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
      Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      If a TextArea contains line separators (e.g. \r\n or \n), any text being replaced with replaceRange() starting from after the line separators is not replaced correctly. Apparently the start position calculation ignores the line separators, and the actual position where the replace is done is after the specified position.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Compile the source code (Test.java)
      2. Run the test class (java Test)
      3. Observe the output

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      A window is shown with three TextAreas, where the character b has been replaced with the character c in each TextArea.
      ACTUAL -
      Only the first TextArea (not containing any line separators) shows the correct text. The two other TextAreas (containing line separators) show the wrong text: the character b has not been replaced but the character c has just been appended to the text..

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.Frame;
      import java.awt.GridLayout;
      import java.awt.TextArea;
      import java.awt.event.WindowAdapter;
      import java.awt.event.WindowEvent;

      public class Test
          extends Frame
      {
          protected Test()
          {
              super("Test application");
              setSize(576, 448);

              addWindowListener(new WindowAdapter()
              {
                  public void windowClosing(WindowEvent windowEvent)
                  {
                      setVisible(false);
                      dispose();
                      System.exit(0);
                  }
              });

              setLayout(new GridLayout(3, 1));

              TextArea textArea1 = new TextArea(5, 80);
              TextArea textArea2 = new TextArea(5, 80);
              TextArea textArea3 = new TextArea(5, 80);
              add(textArea1);
              add(textArea2);
              add(textArea3);
              setVisible(true);

              textArea1.setText("a--b");
              textArea1.replaceRange("c", 3, 4);

              textArea2.setText("a\r\nb");
              textArea2.replaceRange("c", 3, 4);

              textArea3.setText("a\n\nb");
              textArea3.replaceRange("c", 3, 4);
          }

          public static void main(String[] args)
          {
              new Test();
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      There is no acceptable workaround. It's possible to use setText() but it "flashes" the text in the TextArea quite disturbingly when called repeatedly.
      (Incident Review ID: 244728)
      ======================================================================

            art Artem Ananiev (Inactive)
            jleesunw Jon Lee (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: