Swing GUI doesn't update during data processing

允我心安 提交于 2019-12-02 09:28:22

You can do something like this, not the best but it can give you some idea

Create a thread dispatcher class and call it from main class

public class ThreadDispatcher implements Runnable { 

public ThreadDispatcher() {

}

public void run() {

        //call the method related heavy process here 

    }     

}  

It may be like this in your main class

 Thread thread = new Thread(new ThreadDispatcher());
 thread.start();
 sleep(100);

catch the InterruptedException ex.

And check the Java thread examples.

I'm having a problem where my Swing GUI components aren't updating for me while the program is busy.

That is because you are executing a long running task on the Event Dispatch Thread (EDT) and the GUI can't repaint itself until the task finishes executing.

You need to execute the long running task in a separate Thread or a SwingWorker (for a better solution). Read the section from the Swing tutorial on Concurrency for more information about the EDT and examples of using a SwingWorker to prevent this problem.

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