JProgressBar在下载期间不会更新

时间:2013-07-01 09:17:47

标签: java swing concurrency jbutton jprogressbar

使用swing按钮,我正在尝试下载一个html文件并将其写入新的html文件,同时执行进度条。当我单击按钮时,我的程序似乎冻结,直到下载完成,然后进度条突然100%。我不太确定如何解决这个问题,因为我是java新手。

private void jButton2MouseReleased(java.awt.event.MouseEvent evt) {
    try {    
        URL oracle = new URL("http://mywebsite.com");
        URLConnection yc = oracle.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
        String inputLine;
        String input_path = "d:/website/updatedsite.html";
        WriteFile data = new WriteFile(input_path, false);
        int length = yc.getContentLength();
        int current = 0;
        jProgressBar1.setMaximum(length);
        jProgressBar1.setValue(0);
        while ((inputLine = in.readLine()) != null) {
            data.writeToFile(inputLine);
            int i = 0;
            for (i = 0; i < length; i++) {
                jProgressBar1.setValue(i);
            }
        }
        in.close();
    }
    catch (java.io.IOException e) { 
    JOptionPane.showMessageDialog(Frame1.this, "Error " + e.getMessage());
}

}

2 个答案:

答案 0 :(得分:3)

这是因为你在同一个帖子中下载和更新进度条 - 这就是gui在下载完成后实际更新的原因。

使用单独的工作线程进行下载,如解释here,它应该可以正常工作。

答案 1 :(得分:1)

我建议使用SwingWorker。这是为了解决这个问题。以下是我的代码库中使用不确定进度条的一些代码的代码段,但它可以帮助您大致了解要执行的操作(vui是JFrame):

vui.clearOutput();
vui.setOutput("Waiting for items to copy...\n"
            + "This could take several minutes. Please standby...");
vui.disableExit();
vui.disableInput();
vui.disableNext();
vui.showProgressBar();

// make thread so you can disable all options when zipping and enable progress bar
SwingWorker transferWorker = new SwingWorker() {
    @Override
    protected Void doInBackground() throws Exception {
        try {
            Process p = Runtime.getRuntime().exec("resources/bin/transferCopier.bat");

            StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");
            StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), "OUTPUT");
            errorGobbler.start();
            outputGobbler.start();

            p.waitFor();
        } catch (IOException ex) {
            Logger.getLogger(VaderController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ie) {
            Logger.getLogger(VaderController.class.getName()).log(Level.SEVERE, null, ie);
        }
        return null;
    }
    @Override
    public void done() {
        vui.hideProgressBar();
        vui.clearOutput();
        vui.setOutput("Transfer Complete!\n\n"
                    + "Push \"Exit\" to quit.\n\n");
        vui.enableExit();
        mode = VaderMode.END;
    }
};
transferWorker.execute();

执行transferWorker.execute();时,会调用doInBackground()。完成doInBackground()计算后,将调用done()done()是您对GUI进行任何最终更新的地方。

关于我上面代码的关键注意事项是我在执行SwingWorker之前启用了进度条,然后当SwingWorker执行完毕时,我禁用了进度条。

资源:

http://docs.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html