I have some code to specifically download one thing and then more code to download an array of items specified, however for some reason the progress bar does not show up unless I call the first method first and then the second one. However, the gui itself does appear. This shouldn't happen because the code is almost identical except for a few things. Here is the code to the first one:
public void downloadMod(final String url, final String location) {
Thread download = new Thread() {
public void run() {
try {
URL website = new URL(url);
HttpURLConnection connection = (HttpURLConnection)website.openConnection();
int size = connection.getContentLength();
float totalDataRead = 0;
BufferedInputStream input = new BufferedInputStream(connection.getInputStream());
FileOutputStream fileOutput = new FileOutputStream(location);
BufferedOutputStream output = new BufferedOutputStream(fileOutput, 1024);
byte[] data = new byte[1024];
int i = 0;
while((i = input.read(data, 0, 1024)) >= 0) {
totalDataRead = totalDataRead + i;
output.write(data, 0, i);
float percentf = (totalDataRead * 100) /size;
int percent = (int)percentf;
progressBar.setValue(percent);
}
output.close();
input.close();
Zip.unZip(location);
//new File(Start.POWER_LAUNCHER_LOCATION + "modpacks/" + ModPack.getMod(LauncherGUI.selectedMod) + "/pack.json").delete();
new File(Start.POWER_LAUNCHER_LOCATION + "modpacks/" + ModPack.getMod(LauncherGUI.selectedMod) + "/version").delete();
new File(Start.POWER_LAUNCHER_LOCATION + "modpacks/" + ModPack.getMod(LauncherGUI.selectedMod) + "/minecraft/pack.json").renameTo(new File(Start.POWER_LAUNCHER_LOCATION + "modpacks/" + ModPack.getMod(LauncherGUI.selectedMod) + "/pack.json"));
new File(Start.POWER_LAUNCHER_LOCATION + "modpacks/" + ModPack.getMod(LauncherGUI.selectedMod) + "/minecraft/version").renameTo(new File(Start.POWER_LAUNCHER_LOCATION + "modpacks/" + ModPack.getMod(LauncherGUI.selectedMod) + "/version"));
dispose();
new Launch().launch(ModPack.getMod(LauncherGUI.selectedMod));
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, "Error Downloading!");
new File(location).delete();
dispose();
}
}
};
download.start();
}
The second one does almost the exact same thing except with multiple files:
public void downloadCrap(final String[] urls, final String[] locations) {
Thread download = new Thread() {
public void run() {
for (int j = 0; j < urls.length; j++) {
try {
URL website = new URL(urls[j]);
HttpURLConnection connection = (HttpURLConnection) website.openConnection();
int size = connection.getContentLength();
float totalDataRead = 0;
BufferedInputStream input = new BufferedInputStream(connection.getInputStream());
FileOutputStream fileOutput = new FileOutputStream(locations[j]);
BufferedOutputStream output = new BufferedOutputStream(fileOutput, 1024);
byte[] data = new byte[1024];
int i = 0;
while ((i = input.read(data, 0, 1024)) >= 0) {
totalDataRead = totalDataRead + i;
output.write(data, 0, i);
float percentf = (totalDataRead * 100) / size;
int percent = (int) percentf;
progressBar.setValue(percent);
}
output.close();
input.close();
}
catch (Exception e) {
new File(locations[j]).delete();
}
}
}
};
download.start();
try {
download.join();
}
catch(Exception e) {
e.printStackTrace();
}
dispose();
}
I have no idea what I'm doing wrong and help would be appreciated!
You should use a SwingWorker. Check the oracle tutorial for JProgressBar.
http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html