I have been working on a program in java which displays the different steps of sorting a list. My step() method updates the gui with the current iteration of the sort, and works fine when it is called from my button's action listener.

My goal is to make an auto-step, which calls step() and then pauses the thread. My problem is that the gui won't update! I have tried all kinds of different wait functions, plus revalidating, repainting and updating the GUI. The part of the code in question is below.

/* stepFast(delayTime)
	  *
	  *automatically steps through the sort
	  *
	  * @param: amount of time to wait between steps
	  */
	  	public void stepFast(){
			//while there are still steps in the list,
			//keep stepping
			for (int i = 0; i < this.steps.size(); i++){
				step(stepSpeed);
			}
		}

	/*step()
	 *"Steps" to the next square of the maze solution
	 * THIS CODE IS WORKING FOR INDIVIDUAL STEPS
	 */
	 private void step(long pause){
			AnInt[] next = this.steps.removeFirst();
			

			for (int i = 0; i<size; i++){
				this.start[i].setStatus(next[i].status());
				this.start[i].setContents(next[i].contents());
			}

			//if this is the last step, disable the button, 
                        //and show a valid message
			if (this.steps.size() == 0){
				message("The sort has been completed.");
				this.stepButton1.setEnabled(false);
				this.stepButton2.setEnabled(false);
			}

                        //refresh/update/ the gui, and then pause
			aDisplay.updateUI();
			waitLong(pause);
		 }

    		/*wait
		 * pauses for s hundredths of a second
		 */
		public static void waitLong(long s) {
			     try {
			       Thread.currentThread();
			       Thread.sleep(s * 100);
			       }
			     catch (InterruptedException e) {
			       e.printStackTrace();
			       }
		}

I hope someone can show me where I've gone wrong

Edited 6 Years Ago by peter_budo: Correcting code tags

http://java.sun.com/javase/6/docs/api/javax/swing/package-summary.html#threading

Swing threading and the event-dispatch thread
http://www.javaworld.com/javaworld/jw-08-2007/jw-08-swingthreading.html?page=1

I am still fairly new when it comes to threads. Can you explain how to implement what those resources are saying? I have tried pausing without using threads (with a do-while loop which waits for a certain amount of time to elapse) but this does not work either.

http://java.sun.com/javase/6/docs/api/javax/swing/package-summary.html#threading

Swing threading and the event-dispatch thread
http://www.javaworld.com/javaworld/jw-08-2007/jw-08-swingthreading.html?page=1

I am still fairly new when it comes to threads. Can you explain how to implement what those resources are saying? I have tried pausing without using threads (with a do-while loop which waits for a certain amount of time to elapse) but this does not work either.

I tried using the SwingWorker and this does stop the gui from freezing during the "auto-step" but the original problem remains: the gui will not update.

Anyone?

This article has been dead for over six months. Start a new discussion instead.