首页 > 解决方案 > Threads in swing freezing - java

问题描述

I'd like to realize a fake bash where when i append multiple strings on my output (JTextPane), there is a little wait between a message and another. I did it with a thread, but all freeze when I append a new message. This is the code for append:

public void append(String s) {
    Thread t = new Thread(){
        public void run() {
            textPane.setText(textPane.getText()+"\n"+s);
            try {
                Thread.sleep((long) (Math.random()%1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    t.start();
    try {
        t.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

But the result is that all freeze waiting all messages is written. What can I do?

标签: javamultithreadingswingsleepjtextpane

解决方案


推荐阅读