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

后端 未结 4 1128
轻奢々
轻奢々 2021-01-13 10:48

I want to disable a button for a specific time in JavaFX application. Is there any option to do this? If not, is there any work around for this?

Below is my code in

4条回答
  •  猫巷女王i
    2021-01-13 11:03

    You could use the simple approach of a thread that provides the relevant GUI calls (through runLater() of course):

    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();
    

    It's perhaps not the neatest way of achieving it, but works safely.

提交回复
热议问题