У меня есть таймер обратного отсчета, который вызывается при потере подключения к Интернету и повторяет попытку каждые 30 секунд. Когда я пытаюсь запустить его, он зависает в системе. Сообщение об ошибке не выдается.
По попыткам отладки я знаю, что она останавливается после вызова отображения сцены, и сцена не отображается.
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 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();
}
}