3

I am trying to make a gui, whereby users are able to download files. Currently I am able to call the wget command through a process, but I am struggling to use it along with swingworker.

How would I go about tracking the progress of the downloading and updating a gui simultaneously?

Currently I have tried using this method:

ShellProcess.command("wget --progress=dot "+_url);

Where command is the method that creates the process:

InputStream stdout = _process.getInputStream();
    BufferedReader stdoutBuffered =new BufferedReader(new InputStreamReader(stdout));


    String line = null;
    String output ="";
    try {
        while ((line = _stdoutBuffered.readLine()) != null ) {
            //              System.out.println(line);
            output+=(line+" ");
            System.out.println(line +" SHELL");
            _progress++;
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    _progress = 0;
    return output;

}

I am trying to count the amount of lines outputted as "wget --progress=dot" should output a line for every percent of progress. But this does not seem to work.

My doInBackground method inside the swingworker looks like this:

    @Override
protected Integer doInBackground() throws Exception {
    // Start
    download .command("wget "+_url);
    ShellProcess.command("wget --progress=dot "+_url);
    int progress = 0;
    while (progress<101){
        progress = ShellProcess.getProgress() %100 ;
        publish(ShellProcess.getOutput());
        setProgress(progress);

    }
    return 1;
}

Any help would be appreciated.

Ofek
  • 325
  • 1
  • 2
  • 13

2 Answers2

2

In this complete example, the background method of a SwingWorker starts a ProcessBuilder. Standard output and error streams are combined for display in a text component. Substitute your wget command to see the effect. Experiment with --progress=bar and reading a character at a time.

ProcessBuilder pb = new ProcessBuilder("wget", "--progress=dot", url);
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

You don't really need SwingWorker for this. Just download in a separate Thread (don't do it in the EDT), and every time you encounter a new dot line output from wget, update a GUI component (a progress bar for example), but do this update in the EDT e.g. with SwingUtilities.invokeLater():

JProgressBar progressBar = ...; // Initialize and add progress bar to your GUI
...

// In your separate download thread:
final AtomicInteger percent = new AtomicInteger();

while ((line = _stdoutBuffered.readLine()) != null ) {
    if (".".equals(line)) {
        // A new percent was completed, update the progressbar:
        percent.incrementAndGet();

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                progressBar.setValue(percent.get());
            }
        });
    }
}
icza
  • 389,944
  • 63
  • 907
  • 827
  • not invokeAndWait and if yes then must be tested for false from isEventDispatchThread, if yes then invokeLater must be used, reasons 1. invokeAndWait shouldn't be called on EDT, 2. exception from RepaintManager can kill your JVM instance, 3. another 3-5 less important reasons (too lazy to....) – mKorbel Aug 14 '14 at 06:54
  • agree about don't to use SwingWorker but, because is shadowing, unmanagable, awfull API e.i., there are three ways 1. ProgressMonitor 2. Runnable#Thread with invokeLater for setValue 3. SwingWorker – mKorbel Aug 14 '14 at 06:56
  • Sorry I am not very good with threads. I have made ShellProcess implement runnable. The run method calls the command method. Back in the class that instantiates the ShellProcess object,I created an new thread with the ShellProcess in the constructor and I call start() on it. But ShellProcess returns Exception in thread "Thread-0" java.lang.NullPointerException. For the line containing: while ((line = _stdoutBuffered.readLine()) != null ) {. (the download does start...just unable to get progress due to the error) – Ofek Aug 14 '14 at 07:39