title: MVC Progress Bar Threading
body: <p>I am using an MVC pattern for my design, when a user presses the search button, I call a search in the model, but I also want to update a progress bar with information returned from that model.</p>
  
  <p>I have tried using a swingworker, but the progress bar does not update. I suspect I am doing something wrong with my threading.</p>
  
  <p>My button as defined in the controller is:</p>
  
  <pre><code> class SearchBtnListener implements ActionListener {
          public void actionPerformed(ActionEvent e) {
              _view.displayProgress();  
          }    
  }
  </code></pre>
  
  <p>This calls the search in the model and has the following call in the view:</p>
  
  <pre><code>public void displayProgress() {
  
      TwoWorker task = new TwoWorker();
      task.addPropertyChangeListener(new PropertyChangeListener() {
  
           @Override
           public void propertyChange(PropertyChangeEvent e) {
               if ("progress".equals(e.getPropertyName())) {
                  _progressBar.setValue((Integer) e.getNewValue());
               }
           }
  
       });
       task.execute();             
  }      
  
  
  private class TwoWorker extends SwingWorker&lt;Void, Void&gt; {        
      @Override
      protected Void doInBackground() throws Exception {
          _model.startSearch(getTerm());                  // time intensive code
          File file = new File("lock");           
          while (file.exists()){
              setProgress(_model.getStatus());
              System.out.println(_model.getStatus()); // never called
          }           
          return null;
      }  
  
      protected void done(){
          updateMain();
      }
  }
  </code></pre>
  
  <p>Dummy function defined in Model for testing:</p>
  
  <pre><code>public int getStatus(){
      Random r = new Random();
      return r.nextInt();
  }
  </code></pre>