【发布时间】:2014-01-31 13:03:32
【问题描述】:
class ProgressBarSwingWorker extends SwingWorker<Void, Void>
{
private int byteWritten = 0;
private String downloadDir = "";
private String fileAddress = "";
private String fileName = "";
private int fileSize;
public void fileUrlReadAndDownload(String fileAddress, String downloadDir)
{
OutputStream outStream = null;
URLConnection uCon = null;
InputStream is = null;
try
{
message.setText(fileName + "in Update");
URL Url;
byte[] buf;
int byteRead;
Url = new URL(fileAddress);
outStream = new BufferedOutputStream(new FileOutputStream(downloadDir));
uCon = Url.openConnection();
is = uCon.getInputStream();
buf = new byte[size];
while ((byteRead = is.read(buf)) != -1)
{
outStream.write(buf, 0, byteRead);
byteWritten += byteRead;
pbar.setValue(byteWritten);
frame.repaint();
}
message.setText("update finish");
} catch (Exception e)
{
message.setText("server is not runing.");
e.printStackTrace();
} finally
{
try
{
is.close();
outStream.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
public ProgressBarSwingWorker(String fileAddress, String downloadDir, String fileName, int fileSize)
{
this.fileAddress = fileAddress;
this.downloadDir = downloadDir;
this.fileName = fileName;
this.fileSize = fileSize;
}
@Override
protected Void doInBackground() throws Exception
{
pbar.setMaximum(fileSize);
fileUrlReadAndDownload(fileAddress, downloadDir);
return null;
}
@Override
protected void done()
{
try
{
get();
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我让 swingworker 像这样从 http 下载文件并显示进度条 GUI
我创建了 10 个 progreebarswingworker 实例并放入列表并像这样运行
for(ProgressBarSwingWorker p : progressList)
{
p.execute();
}
所有的progressList都是同时开始的
我要一步一步执行等待其他进度完成
所以我把 done() 或 get() 方法放在这样的
for(ProgressBarSwingWorker p : progressList)
{
p.execute();
p.done(); // or p.get();
}
此代码执行进度其他进度已完成
但是! GUI 已冻结,因此我无法显示任何 JLabel 更改或 JProgressBar
如何在没有 gui 冻结的情况下等待其他进度完成?
【问题讨论】:
-
@GeeHad 我在您的链接中使用了代码,但它也不起作用我在线程中附加我的代码我错了吗?
-
“如何在没有 gui 冻结的情况下等待其他进度完成?”这简直是个错误的问题。在 GUI 线程中等待意味着冻结 GUI。 不要在 GUI 线程内等待.
-
@Holger hmm... 以及如何知道 GUI SwingWorker 线程已完成?我要一个一个下载文件
标签: java swing user-interface progress-bar swingworker