Question

class ProgressBarSwingWorker extends SwingWorker<Void, Void>
    {
        private int byteWritten = 0;
        private String downloadDir = "";
        private String fileAddress = "";
        private String fileName = "";
        private int fileSize;

        public void fileUrlReadAndDownload(String fileAddress, String downloadDir)
        {
            OutputStream outStream = null;
            URLConnection uCon = null;
            InputStream is = null;
            try
            {
                message.setText(fileName + "in Update");
                URL Url;
                byte[] buf;
                int byteRead;
                Url = new URL(fileAddress);
                outStream = new BufferedOutputStream(new FileOutputStream(downloadDir));
                uCon = Url.openConnection();
                is = uCon.getInputStream();
                buf = new byte[size];
                while ((byteRead = is.read(buf)) != -1)
                {
                    outStream.write(buf, 0, byteRead);
                    byteWritten += byteRead;
                    pbar.setValue(byteWritten);
                    frame.repaint();
                }

                message.setText("update finish");
            } catch (Exception e)
            {
                message.setText("server is not runing.");
                e.printStackTrace();
            } finally
            {
                try
                {
                    is.close();
                    outStream.close();
                } catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }

        public ProgressBarSwingWorker(String fileAddress, String downloadDir, String fileName, int fileSize)
        {
            this.fileAddress = fileAddress;
            this.downloadDir = downloadDir;
            this.fileName = fileName;
            this.fileSize = fileSize;
        }

        @Override
        protected Void doInBackground() throws Exception
        {
            pbar.setMaximum(fileSize);
            fileUrlReadAndDownload(fileAddress, downloadDir);

            return null;
        }

        @Override
        protected void done()
        {
            try
            {
                get();
            } catch (InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

i make swingworker like this for downloading file from http and show progress bar GUI

i made 10 progreebarswingworker instance and put in list and run like this

for(ProgressBarSwingWorker p : progressList)
{
p.execute();
}

and all progressList is start same time

i want execute step by step wait other progress is finish

so i put done() or get() method like this

for(ProgressBarSwingWorker p : progressList)
    {
    p.execute();
p.done(); // or p.get();
    }

this code execute progress other progress is done

but! GUI is freeze so i can't show any JLabel change or JProgressBar

how to to wait other progress finish without gui freeze?

Was it helpful?

Solution

You are trying something conceptionally wrong. You try to wait for a completion to perform another action which yields to the single-threaded behavior that you want to avoid. It doesn’t matter how you rewrite your code, as long as you always end up trying to wait your GUI will freeze.

You have to change your concept. Instead of waiting you have to tell Swing what to do when the task has finished, i.e. start the next one. You don’t need to invoke get as it has nothing to do with defining the next action. You can either:

  • Tell your ProgressBarSwingWorker the reference to the next one and invoke its execute method inside the done method

    class ProgressBarSwingWorker extends SwingWorker<Object, Object>
    {
      ProgressBarSwingWorker next;
      // …
      public ProgressBarSwingWorker(ProgressBarSwingWorker nextWorker)
      {
        next=nextWorker;
      }
      // …
      @Override
      protected void done()
      {
        if(next!=null) next.execute();
      }
    }
    

    or

  • add a listener which will invoke the execute method of the next ProgressBarSwingWorker

    ProgressBarSwingWorker previous=null;
    for(final ProgressBarSwingWorker p: progressList)
    {
      if(previous==null) p.execute();
      else previous.addPropertyChangeListener(new PropertyChangeListener()
      {
        public void propertyChange(PropertyChangeEvent evt)
        {
          if("state".equals(evt.getPropertyName())
          && evt.getNewValue() == SwingWorker.StateValue.DONE)
            p.execute();
        }
      });
      previous=p;
    }
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top