Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange text wrapping with styled text in JTextPane with Java 7
    text
    copied!<p>I have two different editors using JTextPane with strange bugs in Java 7 that did not occur with the previous JVM versions. It happens with long lines containing styled text or components.</p> <p>Here is an example demonstrating this bug. In this example, the default style is applied for all the text each time a character is inserted. I tested it with the JDK 1.7.0_04.</p> <pre><code>import java.awt.BorderLayout; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; public class BugWrapJava7 extends JFrame { JTextPane jtp; StyledDocument doc; public BugWrapJava7() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); jtp = new JTextPane(); add(jtp, BorderLayout.CENTER); jtp.setText("\ntype some text in the above empty line and check the wrapping behavior"); doc = jtp.getStyledDocument(); doc.addDocumentListener(new DocumentListener() { public void insertUpdate(DocumentEvent e) { insert(); } public void removeUpdate(DocumentEvent e) { } public void changedUpdate(DocumentEvent e) { } }); setSize(200, 200); setVisible(true); } public void insert() { SwingUtilities.invokeLater(new Runnable() { public void run() { Style defaultStyle = jtp.getStyle(StyleContext.DEFAULT_STYLE); doc.setCharacterAttributes(0, doc.getLength(), defaultStyle, false); } }); } public static void main(String[] args) { new BugWrapJava7(); } } </code></pre> <p>My question is : is there something wrong in my code, or is it indeed a new bug introduced in Java 7 ? And if it is a new JVM bug, is there a workaround ?</p> <p>It might be related to <a href="https://stackoverflow.com/questions/8666727">question 8666727</a>, but the problem here lies in the wrong wrapping rather than the appearance of a scrollbar.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload