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

DefaultStyledDocument#getText(i1, i2) does not handle "\r\n" correctly

XMLWordPrintable

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

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

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

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      Service Pack 3

      A DESCRIPTION OF THE PROBLEM :
      If a StyledDocument is got from a JTextPane initialized from a string that contains "\r\n" characters, then String#substring != StyledDocument#getText for the same indices, provided they are greater than the index of the first "\r\n".

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      the result of StyledDocument#getText is the same as the result String#substring
      ACTUAL -
      the result of StyledDocument#getText is different than the result String#substring

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public static void main(String[] args) throws BadLocationException {
              // not ok
              String s = "01234\r\n7890";
              JTextPane pane = new JTextPane();
              pane.setText(s);
              StyledDocument doc = pane.getStyledDocument();
              assert s.length() == doc.getLength();
              System.out.println(s.substring(7, 10));
              System.out.println(doc.getText(7, 10 - 7));
       
              // ok
              s = "01234\n67890";
              pane.setText(s);
              doc = pane.getStyledDocument();
              assert s.length() == doc.getLength();
              System.out.println(s.substring(7, 10));
              System.out.println(doc.getText(7, 10 - 7));
          }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      replace "\r\n" with "\n" when constructing the JTextPane

            peterz Peter Zhelezniakov
            igor Igor Nekrestyanov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: