how to return a list using SwingWorker
- by Ender
I have an assignment where i have to create an Image Gallery which uses a SwingWorker to load the images froma a file, once the image is load you can flip threw the image and have a slideshow play. I am having trouble getting the list of loaded images using SwingWorker.
This is what happens in the background it just publishes the results to a TextArea
// In a thread
@Override
public List<Image> doInBackground() {
List<Image> images = new ArrayList<Image>();
for (File filename : filenames) {
try {
//File file = new File(filename);
System.out.println("Attempting to add: " + filename.getAbsolutePath());
images.add(ImageIO.read(filename));
publish("Loaded " + filename);
System.out.println("Added file" + filename.getAbsolutePath());
} catch (IOException ioe) {
publish("Error loading " + filename);
}
}
return images;
}
}
when it is done I just insert the images in a List<Image> and that is all it does.
// In the EDT
@Override
protected void done() {
try {
for (Image image : get()) {
list.add(image);
}
} catch (Exception e) { }
}
Also I created an method that returns the list called getImages() what I need to get is the list from getImages() but doesn't seam to work when I call execute() for example
MySwingWorkerClass swingworker = new MySwingWorkerClass(log,list,filenames);
swingworker.execute();
imageList = swingworker.getImage()
Once it reaches the imageList it doesn't return anything the only way I was able to get the list was when i used the run() instead of the execute() is there another way to get the list or is the run() method the only way?. or perhaps i am not understanding the Swing Worker Class.