Skip to content

Instantly share code, notes, and snippets.

@Da9el00
Created June 21, 2022 10:56
Show Gist options
  • Save Da9el00/a65db322f5e79203b8cadfc779543b0a to your computer and use it in GitHub Desktop.
Save Da9el00/a65db322f5e79203b8cadfc779543b0a to your computer and use it in GitHub Desktop.
Javafx alert repeating every x seconds
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.util.Duration;
import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;
public class HelloController implements Initializable {
//Change dutation time to change how often the alert is showing
Timeline alertTimer = new Timeline(new KeyFrame(Duration.seconds(5), event ->{
showAlert();
}));
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
alertTimer.play();
}
public void showAlert(){
Platform.runLater( () -> {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Alert!");
alert.setContentText("This is an alert");
Optional<ButtonType> result = alert.showAndWait();
if(result.isEmpty()){
System.out.println("Alert closed");
alertTimer.play();
} else if(result.get() == ButtonType.OK){
System.out.println("OK!");
alertTimer.play();
} else if(result.get() == ButtonType.CANCEL){
System.out.println("Never!");
alertTimer.play();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment