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

  • SEARCH
  • Home
  • 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 8593305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:57:29+00:00 2026-06-11T23:57:29+00:00

I an trying out a HTTP link to download using a GUI which when

  • 0

I an trying out a HTTP link to download using a GUI which when CLicked Start Download button will start download, Download is working but Label is not changing when download is started as declared in ActionListener. Please help me.

Here’s the code

Label should change when download is started and when completed.

public class SwingGUI2 extends javax.swing.JFrame {    
private javax.swing.JLabel jLabel1;
private javax.swing.JButton jButton1;
private URLConnection connect;

private SwingGUI2() {
    setLayout(new FlowLayout());

    jLabel1 = new javax.swing.JLabel();
    jLabel1.setText("");
    add(jLabel1);

    jButton1 = new javax.swing.JButton();
    jButton1.setText("Start Download");
    add(jButton1);

    jButton1.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            jButton1ActionPerformed(e);
        }
    });


    pack();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    jLabel1.setText("Download Started");  //NOT WORKING
    try {
        String DownURL = "http://www.number1seed.com/music/flymetothemoon.mp3";
        URL url = new URL(DownURL);
        connect = url.openConnection();
        File file = new File("download");

        BufferedInputStream BIS = new BufferedInputStream(connect.getInputStream());
        BufferedOutputStream BOS = new BufferedOutputStream(new FileOutputStream(file.getName()));
        int current;
        while((current = BIS.read())>=0) BOS.write(current);
        BOS.close();
        BIS.close();

        jLabel1.setText("Completed");   //NOT WORKING

    } catch (Exception e) {}
}


public static void main(String[] args) throws ClassNotFoundException, InstantiationException {
    try {
        javax.swing.UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (IllegalAccessException ex) {
        Logger.getLogger(SwingGUI2.class.getName()).log(Level.SEVERE, null, ex);
    } catch (UnsupportedLookAndFeelException ex) {
        Logger.getLogger(SwingGUI2.class.getName()).log(Level.SEVERE, null, ex);
    }

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            SwingGUI2 gui = new SwingGUI2();
            gui.setVisible(true);
            gui.setSize(300,200);
            gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    });
}

}

  • 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-11T23:57:30+00:00Added an answer on June 11, 2026 at 11:57 pm

    You are trying to run the download from the Event Dispatcher Thread, causing your GUI to freeze whilst the task is being performed.

    Have a look at SwingWorker to execute the download task as a background thread. Your actionPerformed method should be something like this:

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        jLabel1.setText("Download Started"); 
        SwingWorker sw = new SwingWorker() {
                public Object doInBackground(){
                    try {
                        // ...
                        // Do the whole download thing
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
    
                    jLabel1.setText("Completed");
                    return null;
                }
            };
        sw.execute();
    }
    

    NB: Make sure you do not swallow the exception in this catch, else you won’t be able to see if anything wrong occurs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying out http requests to download a pdf file from google docs using
i have this link but i am trying to figure out how to download
Hi while trying out jni example in this link http://wendro.blogspot.com/2010/03/jni-example-eclipse-dev-cpp.html?showComment=1309930446765#c5048550711511727724 with eclipse Helios windows
I am trying download file http://images.anandtech.com/doci/5434/X79%20Extreme9Box_575px.jpg but not able to download it using HttpUrlConnection,
I'm trying out the example Hello World from this link here: http://www.mulesoft.org/documentation/display/MULE2INTRO/Quick+Start I have
I am trying to download an vcalendar using a java application, but I can't
I'm trying out the Link to Oracle Solaris Studio 12.2: Performance Analyzer http://docs.oracle.com/cd/E18659_01/html/821-1379/afabb.html It
I was planning on trying out Re-Fox: http://www.refox.net/ They have a demo version -
I'm trying out the code at http://www.scala-lang.org/node/112 and I'm getting a match error for
Question: I'm trying out to convert this here: http://support.microsoft.com/kb/828736 to VB.net I got it

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.