I have a couple of thousand lines of code somewhere, and I noticed that my JTextPane is flickering 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 the terminal style GUI component (cmd) -
I think I did all the optimizations I could here, including \nas a final variable, so it won't be built hundreds of times. However, flicker is noticeable and unacceptable. After a few minutes, the component completely freezes. I have to update the component very quickly, and when updating, the panel should scroll down.
I was thinking about creating my own version of JTextPane from scratch, but I would like you to have an easier solution.