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

最后都变了- 提交于 2021-01-20 12:07:45

问题


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;

}

回答1:


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;

}


来源:https://stackoverflow.com/questions/64555374/how-do-you-refresh-javafx-scene-from-inside-an-button-action

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!