Created
June 21, 2022 10:56
-
-
Save Da9el00/a65db322f5e79203b8cadfc779543b0a to your computer and use it in GitHub Desktop.
Javafx alert repeating every x seconds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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