javafx-2 - JavaFX : How to disable a button for a specific amount of time?

标签 javafx-2

我想在JavaFX应用程序中禁用按钮特定时间。有什么选择吗?如果没有,是否有任何解决方法?

下面是我在应用程序中的代码。我尝试了Thread.sleep,但是我知道这不是阻止用户单击下一步按钮的好方法。

nextButton.setDisable(true);
final Timeline animation = new Timeline(
        new KeyFrame(Duration.seconds(delayTime),
        new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                nextButton.setDisable(false);
            }
        }));
animation.setCycleCount(1);
animation.play();

最佳答案

您可以使用提供相关GUI调用的线程的简单方法(当然通过runLater()):

new Thread() {
    public void run() {
        Platform.runLater(new Runnable() {
            public void run() {
                myButton.setDisable(true);
            }
        }
        try {
            Thread.sleep(5000); //5 seconds, obviously replace with your chosen time
        }
        catch(InterruptedException ex) {
        }
        Platform.runLater(new Runnable() {
            public void run() {
                myButton.setDisable(false);
            }
        }
    }
}.start();

这也许不是实现它的最简洁的方法,但是可以安全地工作。

关于javafx-2 - JavaFX : How to disable a button for a specific amount of time?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16919727/

相关文章:

javafx-2 - JavaFX 2.x : Translate mouse click coordinate into XYChart axis value

controls - 如何从 JavaFX 中的 UI 控件检索阶段

java - 窗与窗的互动

java - 如何确定选择了哪个FileChooser ExtensionFilter - javafx

css - 无法让边框在 TableCell javafx 2 中左右或上下不同

javafx-2 - 动态 JavaFX 绑定(bind)

从 Android 远程控制的 JavaFX 程序

javafx-2 - 阻塞父阶段直到子阶段关闭

image - 是否有任何 JavaFX 图像编辑器?

spring - 为什么需要特定的 Spring FXML 加载器