-2

Here's the situation: My code downloads a file from the internet, also displays its size and filename.

The problem is that when I'm downloading that file, nothing appears in that JTextArea and the frame is like "frozen" until the download is complete.

I even tried to put a progress bar using swingworker class (i asked some days ago requesting information and I didn't understand how to "integrate" the swingworker methods you gave me before in my code. I was told that using Matisse is not recommendable at all in that sort of cases. So I'm not able to use swingworker.

I've been researching, and I think that the suitable way for me is using a thread. With, or without a progress bar. Just looking for simple code, I'm a beginner, thanks

Seba Paz
  • 55
  • 3
  • 9
  • 1
    What thread is running your GUI code? Is it the Event Dispatch thread, or the main thread? If the latter, make it the former. – Kon Sep 28 '13 at 01:35
  • 2
    nobody will help until you show some work, post code that makes the request and downloads file, and we'll advice... – Eugen Halca Sep 28 '13 at 01:35
  • You need to learn to use a SwingWorker, pure and simple. Start with a simple thread sleep example til you get it to work or til you can show a small example here. As currently presented though, I doubt anyone can help you any more unless you ask a better (i.e., answerable) question. – Hovercraft Full Of Eels Sep 28 '13 at 01:36
  • My program has 2 "public void" methods, and then a formWindowOpened window listener, used just to call those methods. – Seba Paz Sep 28 '13 at 01:37
  • @EugenHalca OK. I will upload it right now – Seba Paz Sep 28 '13 at 01:38
  • 2
    1) You know that you should be using a SwingWorker, and you really should post your attempt at using this. Else how can we guess what you may be doing wrong? 2) Again, when trying to use a complex new tool like a SwingWorker, try it first with a simple problem, not with your file download code. Get the SwingWorker working right, **then** implement it in your greater program. Divide and conquer. – Hovercraft Full Of Eels Sep 28 '13 at 01:50
  • 2
    You need to take a look at and understand [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) – MadProgrammer Sep 28 '13 at 01:53

3 Answers3

3

This is probably not the cause of your immediate problem, but code like this:

    } catch (MalformedURLException ex) {

    } catch (IOException ioe) { 

    }

is an accident waiting to happen. If any of those exceptions happens, you've told the application to silently ignore them.

Just don't do it!

If you don't know the correct thing to do with a checked exception, declare is as thrown in the method signature. Don't just throw it away.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0
package org.assume.StackOverflow;

import java.io.File;

public class Progress implements Runnable
{
    private File file;
    private long totalSize;
    private int currentProgress;

    public Progress(String filePath, long totalSize)
    {
        this(new File(filePath), totalSize);
    }

    public Progress(File file, long totalSize)
    {
        this.file = file;
        this.totalSize = totalSize;
        new Thread(this).start();
    }

    public int getProgress()
    {
        return currentProgress;
    }

    @Override
    public void run()
    {
        while (file.length() < (totalSize - 100))
        {
            currentProgress = (int) (file.length() / totalSize);
        }
    }
}

How to use it:

new Thread(new Progress(file, totalSize)).start();
Assume
  • 18
  • 4
0

these lines are actually perfoming the downloading and writing

 while (b != -1) {
       b = in.read();
       if (b != -1) {
           out.write(b);
       }
 }

to add progress add something :

 while (b != -1) {
           b = in.read();
           if (b != -1) {
               downloaded += b;
               out.write(b);
           }
     }

and progress is

downloaded / conn.getContentLength() * 100
Eugen Halca
  • 1,775
  • 2
  • 13
  • 26