Results 1 to 4 of 4
  1. #1
    frejon26 is offline Member
    Join Date
    Jan 2008
    Posts
    14
    Rep Power
    0

    Default SwingWorker Opinions

    I have written a console program in java that reads a rather large (un-formatted) text file and formats it. I wrote a class called FormatText which creates a new thread and formats the text while another Thread, ( the controller ) thread can report updates.

    Anyway, long story short, I have updated the program to a GUI with swing and I use the SwingWorker class to create a background thread which uses the FormatText class to load the TextFile.

    By creating a while loop inside of the SwingWorker's doInBackground()
    method...
    Java Code:
    FormatText text = new FormatText(fileName);
    Thread t = new Thread(text); //text is runnable...
    t.start(); //and the run method has the logic...
    while(t.isAlive())
    {
       this.progress = text.getProgress(); //my class has getProgress method
       this.setProgress(progress);
       try{ Thread.sleep(100); } catch(InterruptedException ie){ } 
    }
    this has been the only way for me to successfully report to a JProgressBar on the progress of the file being formatted, the only drawback, is that I feel I am wasting many clock cycles while the program runs...

    I tried passing a reference of the SwingWorker class to the FormatText class but the SwingWorker's setProgress() method is protected... so aside from copying and pasting the run() method of my FormatText class into the doInBackground() method of the SwingWorker is there any kind of work around that I can have another class set the progress of the invoking SwingWorker Object.

    Any ideas or criticism is appreciated.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    hm, a public wrapper method within the SwingWorker whose only job is to call setProgress may work, as long as thread issues are cared for.

  3. #3
    frejon26 is offline Member
    Join Date
    Jan 2008
    Posts
    14
    Rep Power
    0

    Default

    I thought about that, and I think I will do that, but then I would need to...

    1. make my inner class that extends SwingWorker public. rather than a private inner class.

    2. pass not an instance of type SwingWorker, but of the class that actually extends it, in this case, BackgroundTask, to the FormatText class.

    3. include a new public method like update(int i); that will update the progress.

    - thanks Fubarable.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I have to admit that having a SwingWorker and then having an outside class set progress sounds a bit different. Are you sure that this is the best design for your app? Should the outside class that's setting the property actually be called within a SwingWorker itself?

Similar Threads

  1. SwingWorker Problem
    By Berkan in forum Threads and Synchronization
    Replies: 10
    Last Post: 03-11-2010, 03:28 AM
  2. Replies: 8
    Last Post: 02-08-2009, 11:58 PM
  3. Replies: 3
    Last Post: 10-02-2008, 05:48 AM
  4. Opinions on best approach for objects
    By spikey in forum Advanced Java
    Replies: 1
    Last Post: 04-11-2008, 05:20 PM
  5. swingworker
    By musiigedeo in forum AWT / Swing
    Replies: 1
    Last Post: 07-26-2007, 12:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •