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