java jprogressbar hangs during heavy operation

后端 未结 2 826
长发绾君心
长发绾君心 2021-01-29 02:08

I\'m writing a java program and, before I call a method that makes heavy use of the CPU, I display a frame with a JProgressBar on it. Although I display it before the method is

相关标签:
2条回答
  • 2021-01-29 02:58

    You are blocking the EDT from repainting the GUI.

    Read the section from the Swing tutorial on Concurrency in Swing for a full explanation of the problem.

    Also, read the section on "How to Use Progress Bars" for a working example.

    0 讨论(0)
  • 2021-01-29 03:11

    You should call the heavyLoadMethod in a separate thread. You can use a java.util.concurrent.Executor for it:

    Executor executor = java.util.concurrent.Executors.newSingleThreadExecutor();
    executor.submit(new Runnable() { public void run() { heavyLoadMethod();}});
    

    Now the method will be called in a separate thread and the progress bar will be displayed properly.

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