Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
468 views
in Technique[技术] by (71.8m points)

java - How I append text to textarea with using swingworker class?

I want to append text in loop with swingworkerclass in java. for ex :

while(true)
{
     mytextarea.append("sometext");
     if(some_condition)
     {break;}
}

I want this with swingworker because I want to see every uptade on textarea. For this code I only see update when my proccess done.

I dont want swingworker samples for another situations. Please give me some code here.Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

SwingWorker is not right here. Your code is not running in the EDT so you doesn′t see updates. You can use SwingUtilities.invokeLater(...) to execute your code in the EDT. But do not execute the whole while-loop in the EDT because this will block it and no updates / events (repaints, Mouseclicks). Here is a simply code-example:

    while(true) {
        SwingUtilities.invokeLater(new Runnable{
            public void run() {
                textfield.setText(....);
            }
         });
         if(condition) break;
     }

For more Information look at http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html or this book: http://filthyrichclients.org


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...