title: Swing repaint on call
body: <p>I'm using Java Swing to show a problem being solved recursively. </p>
  
  <p>Every time there is a problem that is solved I'd like to repaint my GUI (Solved after if it passes <code>queenSafe</code> method). </p>
  
  <p>However I'm having an issue here.</p>
  
  <p>I understand that the Event handler and graphics is controlled by the same thread so I can't just tell the thread to sleep to update the GUI when the flag is set. So creating a new thread is best? but not so sure on how to implement it. As of right now I'm trying to use a flag to tell the other thread to repaint. But this seems to give me an infinite loop. Any suggestions?</p>
  
  <pre><code>public void queensProblem(){
      this.guiUpdate();
      boolean solved = solveQueenBlindSearch(0);
      if(!solved){
          JOptionPane.showMessageDialog(gui, "Sorry, but this is not solvable");
      }        
  }
  boolean solveQueenBlindSearch(int col)
  {
      if (col &gt;= cb.queens)
          return true;
  
      for (int row = 0; row &lt; cb.columns; row++)
      {
          if (queenSafe(row, col))
          {
              cb.board[row][col].piece = new Queen();
              this.flag = true;
              if (solveQueenBlindSearch(col + 1) == true)
                  return true;
              cb.board[row][col].piece = null;
          }
      }
      return false;
  }
  void guiUpdate() {
      new Thread() {
          public void run() {
              while(true){
                  if(flag){
                      mainLayout.removeAll();
                      mainLayout.add(searches);
                      JPanel newChessboard = cb.drawChessboard();
                      mainLayout.add(newChessboard);
                      mainLayout.repaint();
                      flag = false;
                  }
              }
          }        
     }.run();
  }
  </code></pre>