-3

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.

CompEng
  • 7,161
  • 16
  • 68
  • 122
  • 1
    Please show us what you have tried with the `SwingWorker` class. We can help you if you have a specific problem, but we're not here to [write your code for you](http://albertattard.blogspot.co.uk/2008/09/practical-example-of-swing-worker.html). – Duncan Jones Aug 16 '12 at 07:29
  • There are many tutorials on the Internet that explain how to use `SwingWorker`. We expect you to show some effort abd attempt to follow these and then ask questions here when you are faced with a specific problem. – Duncan Jones Aug 16 '12 at 07:58
  • in internet I dont find my solution? I want to do this(up there). and when I run this while mytextarea not updating. its update when the while broken. I want to fix this – CompEng Aug 16 '12 at 08:06
  • Try having a look at this example http://stackoverflow.com/questions/11927167/jtextarea-appending-problems/11927201#11927201 it shows (a number of times) a `SwingWorker` appending content to a `JTextArea` – MadProgrammer Aug 16 '12 at 08:59

1 Answers1

0

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

Hendrik Ebbers
  • 2,570
  • 19
  • 34
  • 1
    No, it's almost certain that the code *is* running on the EDT, hence the GUI doesn't get a chance to update until the work is complete. `SwingWorker` is indeed appropriate here (although I tend to avoid it because I find its design questionable). – Duncan Jones Aug 16 '12 at 07:45
  • You are right. My example is running in an extra thread / outside the EDT. So you need a thread to trigger the EDT for every textchange. But I think a swingworker is a little bit oversized for this task. – Hendrik Ebbers Aug 24 '12 at 08:16
  • @HendrikEbbers thanks for answer , but could you delete your answer , because I want to delete my question but I cant ,it says you cant delete it it is a answered question – CompEng Mar 17 '14 at 06:01