Niehm
Niehm

Reputation: 143

Jprogress Bar and concurrency

I know there have been tons of questions regarding this, and I went through most of them in the past 2 hours looking for a working answer, none made it.

My dilemma is that I am making a geocoder. My program is just a gui that allows users to chose an excel file containing addresses to be read, and then by pressing another button to have a new excel file created containing said data and the latitude and longitude linked to it. Now my client will most likely run it with thousands of rows at a time which is why I would like to add a progress bar instead of having the program just hanging. But, the issue with that right now is that as expected, the bar isn't updated until the end.

Now I read about swingworker but I can't seem to get how to implement it. Should my actionperformed method do nothing but create a new thread where all the work is done ? If so how would I get the status of the work as it goes ? I am really confused right now on that issue and would appreciate any help.

Thank you.

Upvotes: 0

Views: 59

Answers (1)

nachokk
nachokk

Reputation: 14413

Here you have a complete example i really like to understand how do you use SwingWorker

A basic example :

public class MySwingWorker extends SwingWorker<String,String> {

       @Override
       public String doInBackground() {
           //here you make heavy task in another thread
           // you call publish or setProgress or both
       }

       @Override
       protected void done() {
           //here is call when job is done, you can update here your gui
       }

      public void process(List<String> chunks){
          // this is called when you do publish in the background here
          // you update the gui
      }

}

You execute this method in your actionPerformed if you want with mySwingWorker.execute()

Upvotes: 1

Related Questions