Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
463 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 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...