Update JProgressBar from ExecutorService

后端 未结 1 1753
孤街浪徒
孤街浪徒 2021-01-27 06:16

I am pinging gateways using Java ICMP ping function. To perform fast pinging I am using ExectorService which creates threads for pinging. After address is pinged (or not) I want

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-27 07:18

    First of all, Swing components may not be used from outside of the event dispatch thread. So, the code updating the progress bar must be enclosed inside

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            scanFrameRefrence.progressBar.setValue(value);
        }
    });
    

    Now, to answer the question. If you want to update the progress bar when a task finishes, the easier way is to have the task itself update the progress bar when at the end of its execution.

    Another way is to use an ExecutorCompletionService, which can be notified (thanks to a blocking queue) when each task has finished.

    Also, consider posting actual, compiling code, and respecting Java naming conventions: variables start with a lower-case letter.

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