Long last line in wrappable textarea hangs GUI (bug in java?)
843806Sep 17 2007 — edited Sep 18 2007If I have a JTextarea or JEditorPane with a very long last line the GUI hangs.
I think it is a bug in java. I am really searching fo a workaround.
You can test it with the following program.
Just start the program and delete the last linebreaks (blank lines) one by one.
The gui will hang.
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class TextEdit extends JFrame
{
public static void main(String[] args)
{
TextEdit editor = new TextEdit();
editor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
editor.setVisible(true);
}
// Create an editor.
public TextEdit()
{
super("Swing Editor");
JTextArea area = new JTextArea();
int length = 1024 * 1024;
StringBuffer sb = new StringBuffer(length);
for (int i = 0; i < length; i++)
{
sb.append("A");
}
sb.append("\n");//append one line break
sb.append("\n");//append secondline break
area.setText(sb.toString());
area.setLineWrap(true);
Container content = getContentPane();
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(area);
content.add(scrollPane, BorderLayout.CENTER);
setSize(320, 240);
}
}