how to suspend execution in java using thread

前端 未结 1 1480
你的背包
你的背包 2021-01-26 17:48

i have created a wizard programatically. it contain 3 panels. the second one is devicePane and third one is detailsPane. the third panel consist of a progress bar. i want my pro

相关标签:
1条回答
  • 2021-01-26 18:05

    You can definitly use a thread to execute your task, this is the preferred way of handling a long running task.

    You have multiple options here. All options include making a callback to your wizard, to update the progressbar.

    You can make your own task class wich does exactly this, or you can use the existing SwingWorker. "SwingWorker itself is an abstract class; you must define a subclass in order to create a SwingWorker object; anonymous inner classes are often useful for creating very simple SwingWorker objects."

    Using the swing worker we just learned about you can use something like this:

    SwingWorker<Integer, Integer> backgroundWork = new SwingWorker<Integer, Integer>() {
    
            @Override
            protected final Integer doInBackground() throws Exception {
                for (int i = 0; i < 61; i++) {
                    Thread.sleep(1000);
                    this.publish(i);
                }
    
                return 60;
            }
    
            @Override
            protected final void process(final List<Integer> chunks) {
                progressBar.setValue(chunks.get(0));
            }
    
        };
    
        backgroundWork.execute();
    

    Note that you will have to break your task down into smaller parts to actually be able to display progress.

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