如何使用 SwingWorker 返回列表
我有一个任务,我必须创建一个图像库,它使用 SwingWorker
从文件加载图像,加载图像后,您可以翻转图像并播放幻灯片。我在使用 SwingWorker
获取加载图像的列表时遇到问题。
这是在后台发生的事情,它只是将结果发布到 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;
}
完成后
,我只需将图像插入 List
即可。
// In the EDT
@覆盖 受保护的无效完成(){ 我还
for (Image image : get()) {
list.add(image);
}
} catch (Exception e) { }
创建了一个返回名为 getImages()
的
list
的方法,我需要获取的是 getImages()
中的列表,但是例如,当我调用 execute()
时,它不会工作。
MySwingWorkerClass swingworker = new MySwingWorkerClass(log,list,filenames);
swingworker.execute();
imageList = swingworker.getImage()
一旦到达 imageList
,它不会返回任何内容,我能够获取列表的唯一方法是当我使用 run()
而不是 execute()
时,是否有另一种方法来获取列表,或者是 run()
方法必由之路?。或者也许我不理解 Swing Worker
类。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你有点误解了
SwingWorker
。大部分都是正确的,但是您不能在execute()
之后立即调用getImage()
- 执行调用立即返回,并且您的图像正在工作线程上加载。因此,立即调用getImages()
没有要获取的图像。要解决此问题,您应该在
done()
覆盖中进行图像处理,例如将图像添加到将显示它们的 UI 组件中。I think you misundersand
SwingWorker
a little. Most of it is right, but you cannot callgetImage()
immediately afterexecute()
- the execute call returns immediately, and your images are being loaded on the worker thread. So, the immediate call togetImages()
has no images to fetch.To fix this, you should do your image handling in the
done()
override, e.g. adding the images to the UI component that will display them.您应该了解
SwingWorker
的生命周期。当您调用execute时,您的处理将切换到后台线程,并且当前线程将立即返回。这就是为什么您的getImages
不返回任何内容。来自 javadoc:You should be aware of the life-cycle of the
SwingWorker
. When you call execute, your processing will switch to a background thread and the current thread will return immediately. This is why yourgetImages
does not return anything. From the javadoc:为什么不使用发布进程机制将中间结果从后台工作线程发送到
EDT
?似乎您正在使用SwingWorker#publish(V)
向用户告知任务的进度,并且您可以实现检查线程进度或更新PropertyChangeListener
。更改您的工作线程以将 Images 引用发送到
EDT
并重写SwingWorkerprocess(Listchunks)
方法以在EDT
上接收该 Images,并且更新组件的模型以显示图片。调用 SwingWorker#get() 方法可以阻止 EDT,同时使用发布过程机制,您可以在每次单张照片可用时更新您的 UI,而其他照片仍在从互联网下载或从磁盘加载。
Why don't you use the publish-process mechanism to send intermediate results from the background worker thread to the
EDT
? Seems that you are usingSwingWorker#publish(V)
to tell about the progress of the task to the user and you can achieve checking the thread progress or updating aPropertyChangeListener
.Change your worker to send Images references to the
EDT
and overrideSwingWorkerprocess(List<V> chunks)
method to receive that Images onEDT
, and update a component's model to display the pictures.Calling SwingWorker#get() method can block the EDT, meanwhile using the publish-process mechanism you can update your UI everytime a single photo become available, while the others are still downloading from the Internet or loading from the disk.