使用可更新JProgressBar的Java Swing线程 [英] Java Swing Threading with Updatable JProgressBar
问题描述
首先,我最近一直在使用Java的Concurrency软件包,但是我发现了一个问题,我被困在了。我希望,应用程序和应用程序可以有一个 SplashScreen
,带有状态栏和其他数据的加载。所以我决定使用 SwingUtilities.invokeAndWait(在这里调用splash组件)
。然后 SplashScreen
会出现一个 JProgressBar
并运行一组线程。但我似乎无法很好地处理事情。我查看了 SwingWorker
并尝试将其用于此目的,但线程只是返回。这是一些伪代码。以及我想要达到的要点。
First off I've been working with Java's Concurrency package quite a bit lately but I have found an issue that I am stuck on. I want to have and Application and the Application can have a SplashScreen
with a status bar and the loading of other data. So I decided to use SwingUtilities.invokeAndWait( call the splash component here )
. The SplashScreen
then appears with a JProgressBar
and runs a group of threads. But I can't seem to get a good handle on things. I've looked over SwingWorker
and tried using it for this purpose but the thread just returns. Here is a bit of pseudo code. and the points I'm trying to achieve.
- 拥有一个具有
SplashScreen的应用程序
加载信息时暂停 - 能够在
SplashScreen下运行多个线程
- 让
SplashScreen的进度条
可以更新,直到所有线程完成后才退出。
- Have an Application that has a
SplashScreen
that pauses while loading info - Be able to run multiple threads under the
SplashScreen
- Have the progress bar of the
SplashScreen
Update-able yet not exit until all threads are done.
启动启动画面
try {
SwingUtilities.invokeAndWait( SplashScreen );
} catch (InterruptedException e) {
} catch (InvocationTargetException e) { }
启动画面构造
SplashScreen extends JFrame implements Runnable{
public void run() {
//run threads
//while updating status bar
}
}
我尝试了很多东西,包括 SwingWorkers
,使用CountDownLatch的线程等等。 CountDownLatch实际上以我想要处理的方式工作但我无法更新GUI。当使用 SwingWorkers
时, invokeAndWait
基本上无效(这是他们的目的)或者它不会更新GUI甚至在使用 PropertyChangedListener
时也是如此。如果其他人有一些想法,那么听听他们会很棒。在此先感谢。
I have tried many things including SwingWorkers
, Threads using CountDownLatch's, and others. The CountDownLatch's actually worked in the manner I wanted to do the processing but I was unable to update the GUI. When using the SwingWorkers
either the invokeAndWait
was basically nullified (which is their purpose) or it wouldn't update the GUI still even when using a PropertyChangedListener
. If someone else has a couple ideas it would be great to hear them. Thanks in advance.
我实际上准备发布更好的代码来帮助找到我的解决方案。感谢所有帮助过的人。
I actually got ready to post better code to help out and found my solution. I thank you for all who helped.
推荐答案
无需调用invokeAndWait中的框架,但是你应该像这样更新进度条状态。
No need to call the frame inside invokeAndWait but you should update progress bar state like this.
try {
SwingUtilities.invokeAndWait( new Runnable() {
public void run() {
//update state of the progress bar here
}
});
} catch (InterruptedException e) {
} catch (InvocationTargetException e) { }
这篇关于使用可更新JProgressBar的Java Swing线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!