I have a Java app that uses a SwingWorker to update a label and a progress bar in the GUI and it works nice. However, I’d like to add a feature to this setting.
My swing worker performs a task which has length n, and it repeats that task m times. Right now my GUI just tells me how many time the task has been repeated, but I’d like it to tell me also at what length of the task we are in. Say n=300 and m=50, I’d like something like:
Task 49 is at 248 ()
Task has been repeated 48 times
What should I modify in my SwingWorker?
/**
*
* @author digitaldust
*/
public class Model extends SwingWorker<Integer, Integer> {
private HashMap<String, Number> GUIparams;
private int session;
private int ticks;
Model(HashMap<String, Number> KSMParams) {
GUIparams = KSMParams;
session = (Integer)GUIparams.get("experimentsInSession");
ticks = (Integer)GUIparams.get("howManyTicks");
}
/**
* Actual simulation
*/
@Override
protected Integer doInBackground() throws Exception {
int i=0;
while(!isCancelled() && i<session){
i++;
int ii=0;
while(!isCancelled() && ii<ticks){
// this is n, the task length and I'd like to update the GUI with this value
ii++;
}
System.out.println(i);
// this is m, how many time the task has been repeated, and now it is updated in the GUI
publish(i);
setProgress(i);
Thread.sleep(1000);
}
return i;
}
/**
* Invoked when simulation exits
*/
@Override
protected void done() {
if (isCancelled()) {
Logger.getLogger(Model.class.getName()).log(Level.WARNING, "Experiment session cancelled by user. Closing Session...");
} else {
// do stuff
Logger.getLogger(Model.class.getName()).log(Level.WARNING, "Experiment session ended.");
}
}
}
The second type parameter V in
SwingWorker<T,V>is used for carrying out intermediate results by this SwingWorker’s publish and process methods. This could be your custom class. Here is an example based on posted SSCCE (shortened for clarity):EDIT: example of process method implementation
EDIT: example of UI update
Here is a slightly modified version of the worker implementation, similar to a sample demonstrated in SwingWorker manual. The only changes are introduction of
textAreamember and updatedsetProgress()call indoInBackground().progressproperty is used to update the progress bar,process()is used to update text area.Here is a demo initialization: