简体   繁体   中英

How do you refresh JavaFX scene from inside an button action?

I'm making a sorting algorithm visualizer and i want the UI to update in real time to show how it works, step by step.

But the UI only updates when the sort() method is done, I want it to update when updateHeight() is done

the sort() method is initiated by an onAction

   @FXML
public void sort() throws InterruptedException {
    minHeight = RectHeight[0];
    counter = 0;
    minIndex = 0;
    temp = 0;

    for(int i=1;i<RectList.size();i++){
        System.out.println(RectList.size());
        System.out.println(counter);
        sort2();
    }


}
public void sort2() throws InterruptedException {
    System.out.println("start sort");

    System.out.println(minHeight);



    System.out.println("find min");
    for (int i = counter; i < (RectList.size()); i++) {
        System.out.println("i= " + i);
        if (RectHeight[i] < minHeight) {
            minHeight = RectHeight[i];
            System.out.println("minHeight= " + minHeight);
            System.out.println("minIndex= " + i);
            minIndex = i;
        }
    }

    updateHeight();
    Thread.sleep(500);

}

public void updateHeight() {
    temp = RectHeight[counter];
    RectHeight[counter] = RectHeight[minIndex];
    RectList.get(counter).setHeight(RectHeight[counter]);

    RectHeight[minIndex] = temp;
    RectList.get(minIndex).setHeight(temp);


    counter++;
    minHeight = RectHeight[counter];
    minIndex = counter;

}

Never sleep on the FX Application thread. That thread is responsible for rendering the UI, so by blocking it with Thread.sleep() you prevent the UI from being rendered, as you have observed.

Instead, use the Animation API . A simple Timeline should work here:

@FXML
public void sort() {

    Timeline timeline = new Timeline();
    timeline.getKeyFrames().add(new KeyFrame(Duration.millis(500), e -> sort2()));
    timeline.setCycleCount(rectList.size());
    minHeight = rectHeight[0];
    counter = 0;
    minIndex = 0;
    temp = 0;

    timeline.play();


}

public void sort2() {
    System.out.println("start sort");

    System.out.println(minHeight);



    System.out.println("find min");
    for (int i = counter; i < (rectList.size()); i++) {
        System.out.println("i= " + i);
        if (rectHeight[i] < minHeight) {
            minHeight = rectHeight[i];
            System.out.println("minHeight= " + minHeight);
            System.out.println("minIndex= " + i);
            minIndex = i;
        }
    }

    updateHeight();

}

public void updateHeight() {
    temp = rectHeight[counter];
    rectHeight[counter] = rectHeight[minIndex];
    rectList.get(counter).setHeight(rectHeight[counter]);

    rectHeight[minIndex] = temp;
    rectList.get(minIndex).setHeight(temp);


    counter++;
    minHeight = rectHeight[counter];
    minIndex = counter;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM