如何从按钮动作内部刷新JavaFX场景? [英] How do you refresh JavaFX scene from inside an button action?

查看:99
本文介绍了如何从按钮动作内部刷新JavaFX场景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个排序算法可视化工具,我希望UI能够实时更新以逐步显示其工作原理.

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.

但是UI仅在sort()方法完成时更新,我希望在updateHeight()完成时更新

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

sort()方法由onAction启动

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;

}

推荐答案

请不要在FX Application线程上休眠.如您所见,该线程负责呈现UI,因此通过用Thread.sleep()阻止它可以防止呈现UI.

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.

相反,请使用动画API .一个简单的Timeline应该在这里工作:

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;

}

这篇关于如何从按钮动作内部刷新JavaFX场景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆