Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javafx-2

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 application. I tried Thread.sleep, but i know this is not the good way to stop the user from clicking on next button.

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();
like image 449
Rohan Avatar asked Jun 04 '13 13:06

Rohan


People also ask

Which method can you use to disable a button in JavaFX?

In Swing, we can disable a button like this: JButton start = new JButton("Start"); start. setEnabled(false);

How do I program a button in JavaFX?

You can create a Button by instantiating the javafx. scene. control. Button class of this package and, you can set text to the button using the setText() method.

Do something when button is pressed JavaFX?

To make a button do something in JavaFX you need to define the executable code usually by using a convenience method like setOnAction() . Then, when the button is clicked, JavaFX does the heavy lifting of fetching that pre-defined code, and executing it on the JavaFX Application thread.

How do you check if a button has been pressed in JavaFX?

When a button is clicked, the button object can be accessed by invoking the getTarget() method of the resulting ActionEvent. The button can be identified by using its label text or, if the button has no text, by its id if this was encoded when the button was created.


3 Answers

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.

like image 108
Michael Berry Avatar answered Jan 04 '23 04:01

Michael Berry


You could also be using the Timeline:

  final Button myButton = new Button("Wait for " + delayTime + " seconds.");
  myButton.setDisable(true);

  final Timeline animation = new Timeline(
            new KeyFrame(Duration.seconds(delayTime),
            new EventHandler<ActionEvent>() {
                @Override public void handle(ActionEvent actionEvent) {
                    myButton.setDisable(false);
                }
            }));
  animation.setCycleCount(1);
  animation.play();
like image 33
Uluk Biy Avatar answered Jan 04 '23 05:01

Uluk Biy


The method to disable a JavaFX control is:

myButton.setDisable(true);

You can implement the time logic programmatically in any way you wish, either by polling a timer or by having this method invoked in response to some event.

If you have created this button instance through FXML in SceneBuilder, then you should assign the button an fx:id so that its reference is automatically injected into your controller object during the loading of the scene graph. This will make it easier for you to work with in your controller code.

If you have created this button programmatically, then you'll already have its reference available in your code.

like image 23
scottb Avatar answered Jan 04 '23 04:01

scottb