Java Task Progress Bars in Swing

不羁的心 提交于 2019-12-02 00:50:24

问题


I need a very simple (skeletal) tutorial on progress bars for developing a GUI in Java. The progress bar can be "indeterminate" and ideally have an updateable text label.

Here is some skeletal code that I hope someone can expand upon just enough to get something working.

class MyFunView extends FrameView {

  // class vars and other stuff...

  @Action
  public void excitingButtonPressed() {

     /* I have n files to download */
     // Download file 1, set progress bar status to "downloading file 1"
     // ...
     // Download file n, set progress bar status to "downloading file 2"

  }

} 

Basically, how and what do I wrap my download code in? (I understand there is some kind of subclass of Task required?)

Thanks much in advance.

EDIT:

We all only have so much precious time to be alive, and no approach can be done in as little code or time as I would have hoped, so I will not be using a progress bar.


回答1:


I need a very simple (skeletal) tutorial on progress bars for developing a GUI in Java.

  • How to Use Progress Bars

  • Concurrency in Swing

  • Initial Threads

  • Event Dispatch Thread

  • SwingWorker

  • examples here




回答2:


You can use SwingWorker in conjunction with a JProgressBar.

The other alternative is to execute your task (e.g. Runnable) and periodically update the JProgressBar. Beware though, that in Swing any UI manipulations have to be done on the EDT. SwingWorker has a nice property of taking care of this detail. Your own solution will have to use SwingUtilities.invokeLater,. for example:

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        progressBar.setValue(progressCounter++);
    }
});

Lot of code for a stupid thing, isn't it? Maybe the SwingWorker with a PropertyChangeListener is a better solution in the end...



来源:https://stackoverflow.com/questions/10422468/java-task-progress-bars-in-swing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!