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

Setting some attributes at Caret position breaks word Wrapping

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • 8
    • 7u4, 8
    • client-libs

      FULL PRODUCT VERSION :
      java version " 1.7.0_07 "

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [version 6.1.7601] (32/64bits)
      Mac OS X 10.8.0
      Linux Ubuntu 32/64bits
      Seems it affects all JRE 7

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      This bug cannot be reproduce on JREs < 7

      A DESCRIPTION OF THE PROBLEM :
      In a JEditorPane with a StyledDocument :

      Setting some character attributes on a word where the caret currenty is breaks the word wrapping. This bug is really new with JRE 7, on previous JRE versions, it does not break.

      REGRESSION. Last worked in version 6u31

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      /**
       * $URL$
       *
       * $LastChangedBy$ - $LastChangedDate$
       */
      package java7.bug;

      import java.awt.BorderLayout;
      import java.awt.Dimension;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import javax.swing.JEditorPane;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.JScrollPane;
      import javax.swing.JTextPane;
      import javax.swing.SwingUtilities;
      import javax.swing.Timer;
      import javax.swing.WindowConstants;
      import javax.swing.event.CaretEvent;
      import javax.swing.event.CaretListener;
      import javax.swing.text.BadLocationException;
      import javax.swing.text.DefaultStyledDocument;
      import javax.swing.text.SimpleAttributeSet;
      import javax.swing.text.StyleConstants;
      import javax.swing.text.StyledDocument;

      /**
       * @copyright VizionR, 2009-2010
       * @author Pierre Souchay <pierre.souchay@vizionr.fr> (last changed by $LastChangedBy$)
       * @version $Revision$
       *
       */
      public class JTextPaneBug {

          /**
           * @param args
           */
          public static void main(String[] args) {
              SwingUtilities.invokeLater(new Runnable() {

                  @Override
                  public void run() {
                      final JFrame jf = new JFrame( " JTextPane bug in Java7 " ); //$NON-NLS-1$
                      final StyledDocument doc = new DefaultStyledDocument();
                      final JEditorPane jt = new JTextPane(doc);
                      jt.addCaretListener(new CaretListener() {

                          SimpleAttributeSet notSel = new SimpleAttributeSet();

                          SimpleAttributeSet sel = new SimpleAttributeSet();
                          {
                              StyleConstants.setUnderline(sel, true);
                              StyleConstants.setUnderline(notSel, false);
                          }

                          @Override
                          public void caretUpdate(final CaretEvent e) {
                              SwingUtilities.invokeLater(new Runnable() {

                                  @Override
                                  public void run() {
                                      int p = jt.getCaretPosition();
                                      try {
                                          String txt = doc.getText(0, doc.getLength());
                                          int start = p;
                                          while (start > 0 && !Character.isWhitespace(txt.charAt(start)))
                                              start--;
                                          int end = p;
                                          while (end < txt.length() && !Character.isWhitespace(txt.charAt(end)))
                                              end++;
                                          doc.setCharacterAttributes(0, start, notSel, false);
                                          doc.setCharacterAttributes(start + 1, end - start - 1, sel, false);
                                          doc.setCharacterAttributes(end, txt.length() - end - 1, notSel, false);
                                      } catch (BadLocationException ignored) {
                                          ignored.printStackTrace();
                                      }
                                  }
                              });

                          }
                      });
                      SwingUtilities.invokeLater(new Runnable() {

                          @Override
                          public void run() {
                              try {
                                  doc.insertString(0,
                                                    " The following Text should have correct Word Wrapping: Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. " , //$NON-NLS-1$
                                                   null);
                                  jt.setCaretPosition(1);
                              } catch (BadLocationException e) {
                                  e.printStackTrace();
                              }

                          }

                      });
                      JPanel panel = new JPanel(new BorderLayout());
                      JScrollPane scroll = new JScrollPane(jt);
                      panel.add(scroll, BorderLayout.CENTER);
                      jf.setContentPane(panel);
                      jf.pack();
                      jf.setSize(new Dimension(320, 240));
                      jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                      jf.setVisible(true);

                      {
                          Timer jtimer = new Timer(2500, new ActionListener() {

                              @Override
                              public void actionPerformed(ActionEvent e) {
                                  SwingUtilities.invokeLater(new Runnable() {

                                      @Override
                                      public void run() {
                                          try {
                                              doc.insertString(jt.getCaretPosition() + 1, " ADDED TEXT " , null); //$NON-NLS-1$
                                          } catch (BadLocationException e) {
                                              e.printStackTrace();
                                          }
                                      }
                                  });

                              }
                          });
                          jtimer.setRepeats(false);
                          jtimer.start();
                      }

                  }

              });

          }
      }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Word Wrapping should work even after 2.5 sec when I add some text (may be performed manually by inserting some text with keyword)
      ACTUAL -
      Word Wrapping is broken.

      Note that sometimes, after word wrapping is broken, moving the caret a few words forwards seems to fix partially the problem.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      CUSTOMER SUBMITTED WORKAROUND :
      I have not found any possible workaround except avoid updating the word containing the carret.

      May be related to http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7125737

            malenkov Sergey Malenkov (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: