Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

java - JButton Action Listener progress bar, update without freezing?

I've got a progress bar, when I strike the button, on the button listener I've got a progress bar that updates as something downloads. However, the GUI freezes until the download is complete. How can I get this progress bar to update as the download continues? However, if the download starts when the application is compiled without user interference, the download progresses as the progress bar updates. However, it's the complete opposite on JButton action listener.

How can I use SwingWorker to get this to work?

while((i=in.read(data,0,1024))>=0)
                    {
                    totalDataRead=totalDataRead+i;
                    bout.write(data,0,i);
                    float Percent=(totalDataRead*100)/filesize;
                    currentProgress.setValue((int)Percent);

                    float allP = Percent / 5;
                    all.setValue((int)allP);

                    }

This is only the loop (without catchException), how can I possibly get the GUI to update as it downloads, after the button listener?!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Execute the download in another thread using SwingWorker. Here you have a complete example i really like with progressBar , see setProgress() publish() and process(). When you use setProgress() it's a bound property you can take approach of observer pattern you can register a listener, then when this method is called gets fired and you catch and can update your progressBar and also you decouple components.

Example:

public class MyWorker extends SwingWorker<Integer, String> {

  @Override
  protected Integer doInBackground() throws Exception {
    // Start
    publish("Start Download");
    setProgress(1);

    // More work was done
    publish("More work was done");
    setProgress(10);

    // Complete
    publish("Complete");
    setProgress(100);
    return 1;
  }

  @Override
  protected void process(List< String> chunks) {
    // Messages received from the doInBackground() (when invoking the publish() method)
  }
}

and in client code:

    SwingWorker worker = new MyWorker();
    worker.addPropertyChangeListener(new MyProgressListener());
    worker.execute();

   class MyProgressListener implements PropertyChangeListener {
      @Override
      public void propertyChange(final PropertyChangeEvent event) {
        if(event.getPropertyName().equalsIgnoreCase("progress")) {
          downloadProgressBar.setIndeterminate(false);
          downloadProgressBar.setValue((Integer) event.getNewValue());
        }         
      }
     }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...