public class download {
public static void Download() {
final String saveTo = System.getProperty("user.home").replace("\\", "/") + "/Desktop/";
try {
URL url = null;
url = new URL("http://cachefly.cachefly.net/10mb.test");
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
FileOutputStream fos = new FileOutputStream(saveTo + "10mb.test");
fos.getChannel().transferFrom(rbc, 0, 1 << 24);
} catch (Exception e) {
e.printStackTrace();
}
}
In my other class I have a event listener
public void download_buttonActionPerformed(ActionEvent e) {
download_button.setEnabled(false);
label_status.setText("- Downloading...");
download.Download();
}
When I click the button on my GUI it freezes up and the label & button never change until the file is downloaded:
http://img200.imageshack.us/img200/2435/45019860.png
Do I have to start the download on a new thread or something? If I start it on a new thread is it still possible to use the progress bar? I'm still new to java so I apologize if I'm doing this completely wrong.