0

I have problem about applied SwingWorker with FileReader and my point is I need to implement FileReader with SwingWorker to make my UI Show the text from the file and this is my code

class Read1 extends SwingWorker<String,String>{
    protected Void doInBackground() throws Exception{
        FileReader read = new FileReader("msg.txt");
        BufferedReader in = new BufferedReader(read);
        String s;
        s=in.readLine();
        System.out.println(s);
        return s;
    }
   protected void done()
    {
      try{
       String show;
       show=get();
       textArea.append(show);}catch(Exception e){}}

 public static void main(String args[]) {
   Read1 r = new Form().new Read1();
   r.execute();

However it does not append anything on the UI textarea

anyone have solution? Thank you

Brandon Boone
  • 16,281
  • 4
  • 73
  • 100
user1539677
  • 23
  • 1
  • 5
  • 1
    First of all, never do this: `catch(Exception e){}`. Log your exception and see what's wrong. Second - that code does not even compile. Post a simple working example that reproduces the problem. – npe Jul 23 '12 at 08:24
  • I already compile and run that the output is become gui but it doesn't append anything on the textarea – user1539677 Jul 23 '12 at 08:32
  • Check out [this example](http://stackoverflow.com/questions/7524800/swing-components-freezing-untill-one-component-complete-its-job). – dacwe Jul 23 '12 at 08:33
  • @dacwe I'm already applied this example to my project however it still does not work – user1539677 Jul 23 '12 at 09:06
  • @user1539677: What doesn't work? – dacwe Jul 23 '12 at 09:09
  • First I make Thread sleep in doInBackground then I publish string s from s = in.readline then I create process method by using public void process(List chunks){ for(String c: chunks) Textarea.insert(c + "\n", 0); but it still not work – user1539677 Jul 23 '12 at 09:14
  • Can you verify that the text is been read. This may be a layout issue. Also, when done is called, dump the contents of the `textArea` to the console `System.out.println(textArea.getText())` – MadProgrammer Jul 26 '12 at 01:42

1 Answers1

0

Works just fine for me:

public class Reader extends SwingWorker<List<String>, String> {

    protected List<String> doInBackground() throws Exception {

        ArrayList<String> lstText = new ArrayList<String>(25);

        BufferedReader in = null;
        try {

        FileReader read = new FileReader("Scanner.txt");
        in = new BufferedReader(read);
        String s = null;

        while ((s = in.readLine()) != null) {

            lstText.add(s);
            publish(s);

        }

        } finally {


            try {

                in.close();

            } catch (Exception e) {
            }
        }

        return lstText;

    }

    @Override
    protected void process(List<String> chunks) {

        for (String text : chunks) {

            fldText.append(text + "\n");

        }

    }

    @Override
    protected void done() {
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I want to ask you more , how can I repeat working of the SwingWorker ? – user1539677 Jul 30 '12 at 08:23
  • SwingWorkers are non-reentrant. That means, once you've called `execute`, it can not be `execute`ed again, you will need to create a new instance. However, in the `doInBackground` you can loop - this does mean that the execution will continue, `done` won't be called and `get` will block – MadProgrammer Jul 30 '12 at 08:30
  • So what solution that I need if the situation is I am the receiver and when I finish read this text file then I remove file to other directory and when sender send the new text file my GUI need to show it again without using any button – user1539677 Jul 30 '12 at 08:44
  • If you ave to read the file again, I'd say you'll want to create a new instance of te work, keeps things simple & isolated – MadProgrammer Jul 30 '12 at 10:08