I've got a couple thousand lines of code somewhere and I've noticed that my JTextPane flickers when I update it too much.. I wrote a simplified version here:
import java.awt.*;
import javax.swing.*;
public class Test
{
static JFrame f;
static JTextPane a;
static final String NL = "\n";
public static void main(String... args)
{
EventQueue.invokeLater(new Runnable(){
public void run()
{
f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setSize(400, 300);
f.setLocationRelativeTo(null);
a = new JTextPane();
f.add(new JScrollPane(a));
new Thread(new Runnable(){
public void run()
{
int i = 0;
StringBuffer b = new StringBuffer();
while(true)
{
b.append(++i+NL);
a.setText(b.toString());
a.setCaretPosition(b.length());
try{Thread.sleep(10);}catch(Exception e){}
}
}
}).start();
}
});
}
}
This is for a terminal (cmd) style GUI component--
I think I've made all the optimizations I could here, including having \n
as a final variable so it won't be constructed hundreds of times. Still, the flickering is noticeable and unacceptable. After a few minutes, the component freezes completely. I must update the component very quickly, and the pane must be scrolled to the bottom when updated.
I've been thinking about making my own version of JTextPane from scratch, but I'd like to see if you guys have an easier solution.