Results 1 to 4 of 4
Thread: SwingWorker Opinions
- 04-13-2009, 07:48 AM #1
Member
- Join Date
- Jan 2008
- Posts
- 14
- Rep Power
- 0
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...
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...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){ } }
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.
-
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.
- 04-13-2009, 04:34 PM #3
Member
- Join Date
- Jan 2008
- Posts
- 14
- Rep Power
- 0
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.
-
Similar Threads
-
SwingWorker Problem
By Berkan in forum Threads and SynchronizationReplies: 10Last Post: 03-11-2010, 03:28 AM -
SwingWorker and my small internet page downloader
By jurka in forum AWT / SwingReplies: 8Last Post: 02-08-2009, 11:58 PM -
SwingWorker problem!!! How can I run it 2 times or more?
By davigre in forum AWT / SwingReplies: 3Last Post: 10-02-2008, 05:48 AM -
Opinions on best approach for objects
By spikey in forum Advanced JavaReplies: 1Last Post: 04-11-2008, 05:20 PM -
swingworker
By musiigedeo in forum AWT / SwingReplies: 1Last Post: 07-26-2007, 12:59 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks