JButton Action Listener 进度条,更新不卡顿? [英] JButton Action Listener progress bar, update without freezing?

查看:31
本文介绍了JButton Action Listener 进度条,更新不卡顿?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个进度条,当我点击按钮时,在按钮侦听器上,我有一个进度条,它会随着某些内容的下载而更新.但是,GUI 会冻结,直到下载完成.如何在下载继续时更新此进度条?但是,如果在没有用户干预的情况下编译应用程序时开始下载,则下载会随着进度条更新而进行.然而,它与 JButton 动作监听器完全相反.

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.

如何使用 SwingWorker 使其工作?

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);

                    }

这只是循环(没有 catchException),在按钮侦听器之后,我怎么可能让 GUI 在下载时更新?!

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

推荐答案

使用 SwingWorker 在另一个线程中执行下载.这里你有一个完整的例子,我非常喜欢 progressBar,参见 setProgress() publish()process().当您使用 setProgress() 时,它是一个绑定属性,您可以采用观察者模式的方法,您可以注册一个侦听器,然后在调用此方法时触发并捕获并更新您的 progressBar 并且您还可以解耦组件.

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.

示例:

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)
  }
}

并在客户端代码中:

    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());
        }         
      }
     }

这篇关于JButton Action Listener 进度条,更新不卡顿?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆