1

I am using progress bar in my application where i am downloading content from a webaite and saving it into my database. I have put a progress bar to indicate if the download is completed but the problem is when everything is saved in my database then the progress bar becomes 100%. While downloading it should be like 5%,10%,25% and so on then after all is saved it will become 100%. How will i do this? sample code that i have written

timer = new Timer(interval, new ActionListener() {

public void actionPerformed(ActionEvent evt) {
if (j ==100){
//a beep is heard when download is completed
Toolkit.getDefaultToolkit().beep();
timer.stop();
jButton3.setEnabled(true);
pb.setValue(0);
String str = "<html>" + "<font color=\"#FF0000\">" + "<b>" + "Downloading completed." + "</b>" + "</font>" + "</html>";
lbldownload.setText(str);
            }
j = j + 1;
pb.setValue(j);
        }
});

jButton3.setEnabled(false);
j = 0;
String str = "<html>" + "<font color=\"#008000\">" + "<b>" + "Downloading is in progress......." + "</b>" + "</font>" + "</html>"; 
lbldownload.setText(str);
timer.start();



     }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
stella
  • 47
  • 7

2 Answers2

1

Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead implement a SwingWorker for long running tasks. See Concurrency in Swing for more details.

See also ProgressMonitorInputStream, since this is monitoring a download.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

If you know how long the input data will be, keep track of how much you've already loaded and use the ratio between those two numbers to get the percentage. Report that to your UI periodically so it can up date the progress bar.

If you don't know the expected size, you can't estimate a percentage.

keshlam
  • 7,931
  • 2
  • 19
  • 33