Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8735501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:04:29+00:00 2026-06-13T10:04:29+00:00

I’m trying the code I found on the voted answer from this question: Download

  • 0

I’m trying the code I found on the voted answer from this question: Download file using java apache commons?

It’s a download application, take a little look, (I’m not much familiar with JFrames and ActionEvents)

Download.java

package main;

public class Download extends JFrame implements Runnable{

    public static int total;
    public static int done;

    private static class ProgressListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {

            done = (int)((DownloadCountingOutputStream) e.getSource()).getByteCount();
            jbar.repaint();
            DownloadCountingOutputStream.parent.draw((int)((DownloadCountingOutputStream) e.getSource()).getByteCount());//redraw
            DownloadCountingOutputStream.parent.repaint();
        }
    }

    public static JProgressBar jbar = new JProgressBar(); 



    public void draw(int downloaded){System.out.println("downloaded: "+downloaded+ " Total: "+total);

        if (downloaded== 0){

        Container cont = new Container();       
        setDefaultCloseOperation(3);
        setSize(600, 450);
        setResizable(false);
        setVisible(true);

        cont.add(jbar);     
        jbar.setBounds(40, 50, 500, 50);                
        jbar.setMaximum(total);//The total value of bytes to download
        //jbar.setValue(50);
        add(cont);

        jbar.setVisible(true);  
        }

        jbar.setValue(downloaded);      
            //This should update the value of the progress Bar
    }

    public void run() {


        URL dl = null;
        File fl = null;
        OutputStream os = null;
        InputStream is = null;
        ProgressListener progressListener = new ProgressListener();

        draw(done);

        try {
            fl = new File(System.getProperty("user.home").replace("\\", "/") + "/Desktop/afile.rar");
            dl = new URL("https://dl.dropbox.com/u/48076798/afile.rar");
            os = new FileOutputStream(fl);
            is = dl.openStream();

            total = Integer.parseInt(dl.openConnection().getHeaderField("Content-Length"));

            String total = dl.openConnection().getHeaderField("Content-Length");

            DownloadCountingOutputStream dcount = new DownloadCountingOutputStream(os);
            dcount.setListener(progressListener);
            dcount.setParent(this);

            IOUtils.copy(is, dcount);

        } catch (Exception e) {
            System.out.println(e);
        } finally {
            if (os != null) { 
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } 
            }
            if (is != null) { 
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } 
            }
        }
    }

}

DownloadCountingOutputStream.java

package main;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.io.output.CountingOutputStream;

public class DownloadCountingOutputStream extends CountingOutputStream {

    private ActionListener listener = null;
    public static Download parent;

    public DownloadCountingOutputStream(OutputStream out) {
        super(out);
    }

    public void setListener(ActionListener listener) {
        this.listener = listener;
    }

    public void setParent(Download o){

        parent = o;
    }

    @Override
    protected void afterWrite(int n) throws IOException {
        super.afterWrite(n);
        if (listener != null) {
            listener.actionPerformed(new ActionEvent(this, 0, null));
        }
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:04:30+00:00Added an answer on June 13, 2026 at 10:04 am

    It’s difficult to tell from the code sample you’ve provide…

    The main cause of this problem is trying to update the UI while blocking from the Event Dispatching Thread (EDT).

    It’s important to NEVER do any long running or blocking operations within the EDT as this will prevent repaint requests from been acted upon.

    For more information have a read through Concurrency in Swing

    The example below demonstrates the use of a SwingWorker that provides progress updates that are re-synced with the UI

    public class TestProgress {
    
        public static void main(String[] args) {
            new TestProgress();
        }
    
        public TestProgress() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    ProgressPane progressPane = new ProgressPane();
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(progressPane);
                    frame.setSize(200, 200);
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
    
                    progressPane.doWork();
                }
            });
        }
    
        public class ProgressPane extends JPanel {
    
            private JProgressBar progressBar;
    
            public ProgressPane() {
    
                setLayout(new GridBagLayout());
                progressBar = new JProgressBar();
    
                add(progressBar);
    
            }
    
            public void doWork() {
    
                Worker worker = new Worker();
                worker.addPropertyChangeListener(new PropertyChangeListener() {
                    @Override
                    public void propertyChange(PropertyChangeEvent evt) {
                        if ("progress".equals(evt.getPropertyName())) {
                            progressBar.setValue((Integer) evt.getNewValue());
                        }
                    }
                });
    
                worker.execute();
    
            }
        }
    
        public class Worker extends SwingWorker<Object, Object> {
    
            @Override
            protected Object doInBackground() throws Exception {
    
                for (int index = 0; index < 1000; index++) {
                    int progress = Math.round(((float) index / 1000f) * 100f);
                    setProgress(progress);
    
                    Thread.sleep(10);
                }
    
                return null;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.