Using TimeUnit to sleep in between changing JavaFX Label text

Viewed 103

I'm writing a GUI in JavaFX for a Pokemon game I've been working on. After I click what move I want to use, I want to narrate the attack with a label. There are several messages I want to display separately, so I'm trying to use TimeUnit.SECONDS.sleep() to put a 3-second gap in between each message. When I run the program, it stays on the previous screen (moveOptions in the code), waits 6 seconds and resets to the main screen (battleOptions) without displaying any messages. Why won't it display my labels?

@FXML
HBox battleOptions, moveOptions, attackNarration;

@FXML
Label narration;

public void handleMoveButton() throws InterruptedException {

        this.moveOptions.setVisible(false);                  //makes previous screen invisible
        this.attackNarration.setVisible(true);               //makes the layout containing my messages visible

        this.narration.setText("Pikachu used Tackle");       
        TimeUnit.SECONDS.sleep(3);                           //should display above message for 3 seconds

        this.narration.setText("Foe Pikachu used Tackle");
        TimeUnit.SECONDS.sleep(3);                           //should change to this message for 3 seconds

        this.attackNarration.setVisible(false);              //makes message screen invisible
        this.battleOptions.setVisible(true);                 //makes home screen visible to restart the turn
    }
0 Answers
Related