-1

I have a grid Pane which I want to make visible. I want to then pause the program for 2 seconds and make the grid invisible again. For some reason the grid becomes visible after the thread.sleep that I use in my program.

This all happens inside a button click event.

I tried moving around the thread.sleep, putting them in a new method and using multiple sleep but nothing worked.

gameGrid.setVisible(true) 
gameGrid.setVisible(false) 

Button event:

public void handleButtonGo(ActionEvent Event) throws IOException {    //On go button press

        boolean validation = true;
        try {

            gameGrid.setVisible(true);
            placeShips();        

        }catch (Exception e){

            labelwarning.setText(e.getMessage());   //on error the program will stop trying to place ships and refresh any ships placed so far.
            validation = false;
            //gameGrid.getChildren().clear();
            //BoardSetup();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            gameGrid.setVisible(false);

        }
}

The grid is displayed for a millisecond after the thread.sleep.

guest123
  • 3
  • 1
  • 1
    Look into JavaFX Animation Class. A lot of helpful stuff. More specifically PauseTransition. – SedJ601 May 15 '19 at 18:32
  • 1
    Learn about `TimeLine` and `PauseTransition`. https://stackoverflow.com/questions/9966136/javafx-periodic-background-task. In the `PauseTransition` example, you don't need `wait.playFromStart();`. – SedJ601 May 15 '19 at 18:33

1 Answers1

1

Use PauseTransition.

public void handleButtonGo(ActionEvent Event) throws IOException {    //On go button press

        boolean validation = true;
        try {

            gameGrid.setVisible(true);
            placeShips();        

        }catch (Exception e){

            labelwarning.setText(e.getMessage());   //on error the program will stop trying to place ships and refresh any ships placed so far.
            validation = false;
            //gameGrid.getChildren().clear();
            //BoardSetup();
            PauseTransition wait = new PauseTransition(Duration.seconds(2));
            wait.setOnFinished((e) -> {
                /*YOUR METHOD*/      
                gameGrid.setVisible(false);
            });
            wait.play();        

        }
}
SedJ601
  • 12,173
  • 3
  • 41
  • 59