Why does this countdown timer stage crash?

22 hours ago 3
ARTICLE AD BOX

I have a countdown timer that is called when internet connection is lost, it is meant to retry every 30 seconds. When I try to run it, it freezes the system. No error message is given.

I know from attempts to debug that it stops after the call to show the stage, and the stage is not shown.

import java.time.temporal.ChronoUnit; import static javafx.scene.paint.Color.rgb; public class StageCountdown { public static void startTimer() { Stage stage = new Stage(); // CREATE GUI ELEMENTS Label noArtists = new Label(); noArtists.setFont(new Font("Arial", 20)); noArtists.setTextFill(rgb(50, 20, 255)); noArtists.setText("Unable to obtain similar artists."); Label countdownLabel = new Label(); countdownLabel.setFont(new Font("Arial", 16)); countdownLabel.setTextFill(rgb(50, 20, 255)); MyButton btnCancel = MyButton.create("Cancel", 80); GridPane GP = new GridPane(); GP.minHeight(150); GP.minWidth(500); GP.setStyle("-fx-background-color: #6e42ad;"); GP.setVgap(15); GP.setHgap(5); GP.setAlignment(Pos.CENTER); //TIMELINE // Create duration property for time remaining: ObjectProperty<java.time.Duration> remainingDuration = new SimpleObjectProperty<>(java.time.Duration.ofSeconds(30)); // Here: 15 sec count down // Binding with media time format (hh:mm:ss): countdownLabel.textProperty().bind(Bindings.createStringBinding(() -> String.format(" Retrying in %02d seconds", remainingDuration.get().toSecondsPart()), remainingDuration)); GP.add(noArtists, 0, 0); GP.add(countdownLabel, 0, 1); GP.add(btnCancel, 1, 3); // Create time line to lower remaining duration every second: Timeline countDown = new Timeline(new KeyFrame(Duration.seconds(1), (ActionEvent event) -> remainingDuration.setValue(remainingDuration.get().minus(1, ChronoUnit.SECONDS)))); // Set number of cycles (remaining duration in seconds): countDown.setCycleCount((int) remainingDuration.get().getSeconds()); countDown.setOnFinished(event -> stage.close()); // Prepare and show stage: Scene scene = new Scene(GP); stage.setScene(scene); stage.setTitle("No internet Connection"); stage.setWidth(400); stage.setHeight(150); stage.show(); // Start the time line: countDown.play(); } }
Read Entire Article