JProgressBar doesn't update during download

徘徊边缘 提交于 2019-12-13 15:23:44

问题


Using a swing button, I'm trying to download an html file and write it to a new html file, while doing a progress bar. When I click the button, my program seems to freeze until the download finishes, and then the progress bar is suddenly 100%. I'm not quite sure how to fix this problem as I am new to 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());
}

}


回答1:


This is because you're both downloading and updating the progress bar in the same thread - that's why the gui gets actually updated AFTER the download is finished.

Use a separate worker thread for downloading like explained here and it should work.




回答2:


I'd suggest using a SwingWorker. It's made for problems such as this. Here is a snippet from some code from my codebase that uses an indeterminate progress bar, but it'll help give you a general idea of what to do (vui is a 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();

When transferWorker.execute(); is performed, doInBackground() is invoked. Once doInBackground() is done doing its computations, done() is then invoked. done() is where you would do any final updates to the GUI.

The key things to note about my code above is that I enable the progress bar before executing the SwingWorker then when the SwingWorker is done executing, I disable the progress bar.

Resources:

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



来源:https://stackoverflow.com/questions/17400982/jprogressbar-doesnt-update-during-download

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!