0

I am using an external library to download files from Mega but I need a progressbar to display the duration of the download to the user. I have read a lot of progressbar related articles on stackoverflow but the problem is that I don't know what take so long in my code to add a progress bar.

Where and how do I have to add a progressbar to display the download status?

    try {

        URLConnection urlConn = new URL(file_url).openConnection();
        print(file_url);
        is = urlConn.getInputStream();
        while ((read = is.read(buffer)) > 0) {
            cos.write(buffer, 0, read);
        }
    } finally {
        try {
            cos.close();
            if (is != null) {
                is.close();
            }
        } finally {
            if (fos != null) {
                fos.close();
            }
        }
    }
    print("Download finished");
}
Engin
  • 135
  • 1
  • 10

1 Answers1

0

You can build a progress bar dialog and preventively show it, and then delegate your costly behind-the-scenes download process to a SwingWorker.

The SwingWorker is a kind of Runnable object (it will be executed behind your main thread) and it is specially conceived to update graphic elements (publish changes to them) as it does its work.

Have a look at this stackoverflow question:

SwingWorker ProgressBar

Community
  • 1
  • 1
Jorge_B
  • 9,712
  • 2
  • 17
  • 22
  • Do you know what takes so long in the code, because its not my code? – Engin Feb 17 '14 at 12:22
  • Sorry, I didn't understand you had any performance problem. If you think your code is not performing right, you should try to profile it (there are tools for that in your favorite IDE, or you could try to do it manually adding traces with the time spent for each task). With that information (how much time you spend and where), we can think about optimizing it. – Jorge_B Feb 17 '14 at 13:32
  • I know the edited code part takes long but I don't know how to use a progressbar in this case. I have removed the insignificant portion of code, so I hope it will be easier to you to explain it. – Engin Feb 17 '14 at 21:55