# setting up swingworker

This topic is 1978 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to set up a swing worker that does some file processing in the background while updating progress to a JtextArea.

From class X I call swingworker FileProcessor

{ JTextArea outerTextArea; File selectedFile FileProcessor task = new FileProcessor(selectedFile, outerTextArea); selectedFileHolder = task.execute(); }

Fileprocessor Internals

class FileProcessor extends SwingWorker<File, JTextArea> { FileProcessor(File INPUT, JTextArea innerTextArea) { //initialize } @Override public File doInBackground() { File convertedFile = new File("convertedFile.txt"); int counter=0; IoUtil.assertFileIsReadable(INPUT); final GenericFileReader reader = new GenericFileReader(INPUT); while(somecondition==condition) { //processing stuff counter++; } return convertedFile; } }

A few questions

It keeps telling me INPUT isn't recognized in the FileProcessor class.

How do I get convertedFile in FileProcessor to be selectedFileHolder in Class X?

How would I get counter in FileProcessor to constantly update to outerTextArea in class X?

##### Share on other sites
First thing is to fix the generic parameters used by the SwingWorker.
It should be SwingWorker< "Result Type", "Publish Type" >

"Result Type" is the class of the final result we want from running this task.
"Publish Type" is the class encapsulating data we're going to send to GUI components.

In this case, the result we want is convertedFile; which is a File. ("Result Type").
The data we want to send to the UI (JTextArea) is counter, which is an int. ("Publish Type).
NB: Java Generics can't use primitive types directly, instead must use their equivalent wrapper class.

e.g.
 class FileProcessor extends SwingWorker<File, Integer> 

It keeps telling me INPUT isn't recognized in the FileProcessor class.

In the constructor of FileProcessor, you need to store the object reference to "INPUT" as an
instance variable to be used later by "doInBackground()".

How would I get counter in FileProcessor to constantly update to outerTextArea in class X?

This can be done using "publish/process" methods of SwingWorker.
First you also need to store an object reference to the JTextArea.

The complete constructor will look something like this:
 private File INPUT; private JTextArea innerTextArea; FileProcessor(File INPUT, JTextArea innerTextArea) { //initialize this.INPUT = INPUT; this.innerTextArea = innerTextArea; } 

Next, whenever counter is changed; you pass it's new value to the publish method.

e.g.
 while(somecondition==condition) { //processing stuff counter++; publish( counter ); // Tell the GUI about counter. } 

Finally, override the "process" method which receives the new counter values.
Notice that process gets a list of all pending values; rather than been invoked once per publish.

e.g.
 @Override protected void process(List<Interger> chunks) { for (Integer i : chunks) { innerTextArea.append("counter = " + i + "\n"); } } 

How do I get convertedFile in FileProcessor to be selectedFileHolder in Class X?

There are at least 3 differrent ways todo this. The best way depends on what you want to do with "convertedFile".

1. The simplest is to use "get" method of SwingWorker.
But this will block the current thread until the SwingWorker is finished. (You probably don't want that!).

e.g.
 FileProcessor task = new FileProcessor(selectedFile, outerTextArea); task.execute(); selectedFileHolder = task.get(); 

2. You can make FileProcessor an inner class of X, override the "done()" method of SwingWorker.

e.g.
 @Override protected void done() { selectedFileHolder = get(); } 

3. You can add a "PropertyChangeListener" to the SwingWorker, and wait to be notified it's finished.

e.g.
 final FileProcessor task = new FileProcessor(selectedFile, outerTextArea); task.addPropertyChangeListener( new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { if (task.getState() == StateValue.DONE) { selectedFileHolder = task.get(); } } } ); task.execute();  Edited by Angex

##### Share on other sites
 task.addPropertyChangeListener( new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { if (task.getState() == StateValue.DONE) { selectedFileHolder = task.get(); } } } );

When I insert this it says (the method addPropertyChangeListener(PropertyChangeListener) in the type Swingworker<File,Integer> is not applicable for the arguments new PropertyChangeListener(){})

##### Share on other sites
Sorry I didn't check the code compiled properly.

Make sure to include an import statement for the PropertyChangeListener

 import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; 

Also SwingWorker.get(), declares some checked exceptions so it needs to be inside a try-catch block.

 task.addPropertyChangeListener( new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { if (task.getState() == StateValue.DONE) { try { selectedFileHolder = task.get(); } catch (InterruptedException exp) { exp.printStackTrace(); } catch (ExecutionException exp) { exp.printStackTrace(); } } } } );  Edited by Angex

##### Share on other sites
Thanks for your help so far....getting there but the compiler still has issues with

 try { selectedFileHolder = task.get(); } 

it says Cannot refer to a non-final variable selectedFileHolder inside an inner class defined in a different method. When I made selectedFileHolder final it switched to

The final local variable selectedFileHolder cannot be assigned, since it is defined in an enclosing type

EDIT: For some reason another error just popped up near the top at

import java.util.Vector;

Internal compiler error java.lang.IllegalArgumentException: info cannot be null at org.eclipse.jdt.internal.compiler.codegen.StackMapFrame.addStackItem(StackMapFrame.java:81)

Maybe this is a deprecated import? Edited by Jarwulf