Java Task Progress Bars in Swing

前端 未结 2 551
故里飘歌
故里飘歌 2021-01-21 02:34

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.

相关标签:
2条回答
  • 2021-01-21 03:00

    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...

    0 讨论(0)
  • 2021-01-21 03:03

    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

    0 讨论(0)
提交回复
热议问题